Spring4搭建+REST在Spring上搭建

注意,该文是基于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。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springPoC1</display-name>
  
  <servlet>
	<servlet-name>rest</servlet-name>
	<servlet-class>
		org.springframework.web.servlet.DispatcherServlet
	</servlet-class>
	<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>rest</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

rest-servlet.xml
通过component-scan配置,调用AppConfig。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 <context:component-scan base-package="com.test" />
 <mvc:annotation-driven />
</beans>

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")
@EnableWebMvc
public 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测试

安装使用见另一博文《Postman - REST测试利器



参考:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值