通过spring mvc搭建一个提供Restful API的web服务。
准备工作
下载并安装Eclipse ee版本(4.5.2)
下载并安装Tomcat 8.0
-
下载spring framework 4.3.3
http://repo.spring.io/release/org/springframework/spring/4.3.3.RELEASE/
下载commons-logging-1.2.jar
-
下载jackson的三个jar包
jackson-annotations-2.7.0.jar
jackson-core-2.7.5.jar
jackson-databind-2.7.5.jar
Eclipse添加Tomcat
解压tomcat
进入Eclipse首选项,在Server-Runtime Environments添加刚解压的tomcat
创建web工程
-
通过Eclipse创建Dynamic Web Project
工程名:sh
target runtime选择Apache Tomcat v8.0
勾选自动生成web.xml
-
配置lib
拷贝spring framework相关jar包、jackson三个jar包、commons-logging到WEB-ING/lib下
-
配置web.xml
添加spring 调度servlet
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
添加encodingFilter
```xml
<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>
```
4. 配置springmvc-servlet.xml
在WEB-INF下新建springmvc-servlet.xml,添加如下配置:
指定spring在哪个报下扫描controller组件
```xml
<context:component-scan base-package="sh.controller" />
```
指定jsp路径(该配置和restful没有直接联系属于mvc常规配置)
```xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
```
指定contoller输出转换器
```xml
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter"/>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
```
5. 编写restful api
创建包sh.controller放置控制器
创建包sh.vo放置前端数据对象
在sh.vo下创建简单的Bean Person.java,用于测试java对象与json间的自动转换
```java
public class Person {
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}
```
在sh.controller下创建restful的控制器。方法参数加上RequestBody注解才能自动将json格式的数据转为输入对象,ResponseBody注解用来控制返回结果作为输出内容。
```java
@Controller
public class HelloController {
@RequestMapping(value = "/person", method = RequestMethod.POST)
public @ResponseBody
Person addPerson(@RequestBody Person person) {
return person;
}
@RequestMapping(value = "/person", method = RequestMethod.GET)
public @ResponseBody
List<Person> getAllPerson() {
List<Person> list= new ArrayList<Person>();
Person p = new Person();
p.setAge(1);
p.setName("rbg");
list.add(p);
return list;
}
}
```