1、搭建maven项目
用maven创建web项目,webapp项目结构如下:
2、配置maven的pom.xml文件
导入springmvc、junit、json的jar包。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zz</groupId>
<artifactId>serviceDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>serviceDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
</dependencies>
<build>
<finalName>serviceDemo</finalName>
</build>
</project>
3、在web.xml中定义DispatcherServlet来响应请求
定义字符过滤器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>spring mvc, restful, restTemplate, web service</display-name>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
</web-app>
3、定义dispatcherServlet的配置文件
example-servlet.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zz.controller"/>
<mvc:annotation-driven />
</beans>
4、定义Controller类来处理请求
HelloWorldController.java
package com.zz.controller;
import org.springframework.stereotype.Controller;
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.ResponseBody;
@Controller
public class HelloWorldController {
@RequestMapping(value="/say/{name}", produces = "application/json; charset=UTF-8")
public @ResponseBody String say(@PathVariable String name) {
System.out.println(name);
return "hi, " + name;
}
@RequestMapping(value="/add/{name}", method=RequestMethod.POST, produces="application/json; charset=UTF-8")
public @ResponseBody String add(@RequestBody String setJson, @PathVariable String name) {
System.out.println(setJson + ", name: " + name);
return setJson;
}
}
5、测试
RestTest.java
package com.zz.restTemplate;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
public class RestTest {
@Test
public void get() {
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/serviceDemo/example/say/{name}", String.class, "周娜");
System.out.println(response);
}
@Test
public void post() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
Set<String> set = new HashSet<>();
set.add("aa");
set.add("周周");
HttpEntity<String> formEntity = new HttpEntity<String>(set.toString(), headers);
String response = restTemplate.postForObject("http://localhost:8080/serviceDemo/example/add/{name}", formEntity, String.class, "康,峰");
System.out.println(response);
}
@Test
public void t1() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
Set<String> storeIdSet = new HashSet<>();
storeIdSet.add("96e90d74-5b02-455e-b0fd-daeda87b8f4f");
System.out.println(JSON.toJSONString(storeIdSet));
HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSONString(storeIdSet), headers);
String url = "http://127.0.0.1:8080/pyc_search_searchservice/service/search/recommendByIMEIORAccountIDAndStoreIds/{imei}/{accountId}/{indexField}/{queryPage}/{pageSize}/{dataType}";
String response = restTemplate.postForObject(url, formEntity, String.class, "cookie_1518495197133", "null", "null", 1, 10, "all");
System.out.println(response);
}
@Test
public void t2() {
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:8080/pyc_search_searchservice/service/search/suizhiSearchByIMEIORAccountID/{imei}/{accountId}/{indexField}/{queryPage}/{pageSize}/{dataType}";
String response = restTemplate.getForObject(url, String.class, "cookie_1518495197133", "null", "null", 1, 10, "all");
System.out.println(response);
}
}