博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring4搭建+REST在Spring上搭建
阅读量:4203 次
发布时间:2019-05-26

本文共 3970 字,大约阅读时间需要 13 分钟。

注意,该文是基于Spring4而不是Spring boot,因而非零配置文件。

1、Server端 整体架构

项目名对应容器(Tomcat)部署项目的名字,从而访问根URI为:http://localhost:8080/<your-application-name>

2、Lib

commons-logging可以在http://commons.apache.org/proper/commons-logging/download_logging.cgi 下载

3、配置

web.xml

通过servlet-name配置,调用<servlet-name>-servlet.xml。

springPoC1
rest
org.springframework.web.servlet.DispatcherServlet
1
rest
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp

rest-servlet.xml

通过component-scan配置,调用AppConfig。

AppConfig.java (需是代码,实为配置)

package com.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;/* * http://www.tuicool.com/articles/rA7Bfu * http://blog.csdn.net/leecho571/article/details/6559758 * http://www.tuicool.com/articles/j6nIzu * http://www.yiibai.com/spring_mvc/spring-mvc-4-restful-web-services-crud-example-resttemplate.html */@Configuration@ComponentScan("com.test")@EnableWebMvcpublic class AppConfig {	private String test = "";	private String tes1 = test;}

4、代码

GreetingController.java

package com.test;import java.util.concurrent.atomic.AtomicLong;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.util.UriComponentsBuilder;@RestController@RequestMapping("/service")public class GreetingController {	private static final String template="Hello,%s";    private final AtomicLong counter = new AtomicLong();     @RequestMapping("/greeting")    public void greeting(@RequestParam(value="name",defaultValue="World") String name){        System.out.println(counter.getAndIncrement() + String.format(template, name));    }        @RequestMapping(value = "/user1", method = RequestMethod.POST)    public String postUser(@RequestBody String param, UriComponentsBuilder ucBuilder) {    	System.out.println("Creating User " + param);    	return param;    	    }        @RequestMapping(value = "/{name}", method = RequestMethod.GET)    public String getGreeting(@PathVariable String name) {    	String result="Hello "+name;      	return result;    }    }

5、Client端 Lib

6、Client端 代码

SpringRestTestClient.java

import java.net.URI;import org.springframework.web.client.RestClientException;import org.springframework.web.client.RestTemplate;public class SpringRestTestClient {	public static final String REST_SERVICE_URI = "http://localhost:8080/springPoC1/service";		private static void listAllUsers(){		RestTemplate restTemplate = new RestTemplate();		String jsonParam = "{\"name\":\"Aiya\"}";		try {			URI uri = restTemplate.postForLocation(REST_SERVICE_URI+"/user1", jsonParam, String.class);			if (null != uri){				System.out.println("Location : "+uri.toASCIIString());			} else {				System.out.println("uri is null~");			}			String jsonResult = restTemplate.postForObject(REST_SERVICE_URI+"/user1", jsonParam, String.class);			System.out.println("testUser~" + jsonResult);		} catch (RestClientException e) {			e.printStackTrace();		}	}		public static void main(String[] args) {		listAllUsers();	}}
 7、Postman测试

安装使用见另一博文《》

参考:

http://www.tuicool.com/articles/rA7Bfu

http://blog.csdn.net/leecho571/article/details/6559758
http://www.tuicool.com/articles/j6nIzu
http://www.yiibai.com/spring_mvc/spring-mvc-4-restful-web-services-crud-example-resttemplate.html

你可能感兴趣的文章
Node.js Buffer模块深入理解Buffer.allocUnsafe与 Buffer.from及其字符串与Buffer之间的转换
查看>>
(dfs)求[1,2,2,3]的全排列(1.backRemove才行 2.Arraylist可以contain判断包含另外一个Arraylist 3.js版本3个坑,4个人,求全排列)
查看>>
cocos creator长驻节点及其接口addPersistRootNode含义(挂到scene节点,并且添加不销毁标记)
查看>>
数据库备份与还原: 1.mysqldump备份 source还原 2.heidisql64.r5138 备份mysql数据
查看>>
mysql为已经定义好的表添加主键: ALTER TABLE t_user ADD PRIMARY KEY (`uid`);
查看>>
node.js异步问题终极解决: 1.async+await解决回调地狱 2.async+await+Promise.all+try catch用于并发执行,并处理异常
查看>>
面向对象之cc.class子类调用父类的方法(IFTTTSoundAnim.prototype.do_right_action(data, then))
查看>>
c++编译器调用C函数,必须加 extern "C" {}(不要变名字) 不然报错(原因: c++有函数重载机制导致的命名改变导致函数找不到)
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(一)~产品需求
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(二)~技术选型
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(三)~准备工作~mysql,elasticsearch,kafka,redis的安装
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(四)~基础搭建~maven工程管理
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(六)~基础搭建~启动第一个springboot工程server-user
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(五)~基础搭建~cloud、cloud alibaba和boot的版本选择
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(七)~基础搭建~springboot整合druid和mybatisPlus
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(八)~基础搭建~springboot整合swagger接口文档
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(九)~基础搭建~参数校验框架的使用
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(十)~基础搭建~自定义异常、统一结果集和全局异常处理器
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(十一)~基础搭建~alibaba nacos的安装
查看>>
用SpringCloud Alibaba搭建属于自己的微服务(十二)~基础搭建~alibaba nacos的服务注册和发现
查看>>