最近学习了Spring,学习环境为Spring4.3.8 + Eclipse + JDK1.8。
使用Spring开发了一个简单的RESTful服务,客户端的请求和服务端的答复都是json格式。
步骤如下:
1、建立一网站。路径:File->New->Web->Dynamic Web Project。
2、导入Spring的jar包。红框内的jar包是必须要导入的,其他的jar包根据需要导入。
3、配置Web.xml。需要在Web.xml中配置Spring的servlet。
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在Web项目中,Spring的配置文件(springmvc-servlet.xml)一般放在WEB-INF下面。其名称一般命名规则为servlet-name+"-servlet.xml",如果springmvc-servlet.xml。
4、创建Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 扫描该包下类,生成bean -->
<context:component-scan
base-package="com.xhjz.controller" />
<!-- 注解映射的支持。如:将json转成对象(输入),或将对象转成json(输出) -->
<!-- 此方法对Spring4.0以后的不适用,改成方式2 -->
<!-- 方式1-->
<!-- <mvc:annotation-driven /> -->
<!-- 方式2 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</beans>
5、创建JSON模型类
package com.xhjz.controller;
public class MarketCfg {
private String cfgId;
private String cfgName;
public String toString() {
return "cfgId:" + cfgId + "|cfgName:" + cfgName;
}
public String getCfgId() {
return cfgId;
}
public void setCfgId(String cfgId) {
this.cfgId = cfgId;
}
public String getCfgName() {
return cfgName;
}
public void setCfgName(String cfgName) {
this.cfgName = cfgName;
}
}
package com.xhjz.controller;
public class Result {
private String RETCODE;
private String RETMSG;
public String getRETCODE() {
return RETCODE;
}
public void setRETCODE(String rETCODE) {
RETCODE = rETCODE;
}
public String getRETMSG() {
return RETMSG;
}
public void setRETMSG(String rETMSG) {
RETMSG = rETMSG;
}
public String toString() {
return "{\"RETCODE\":\"" + this.RETCODE + "\",\"RETMSG\":\"" + this.RETMSG + "\"}";
}
public static Result create() {
Result rst = new Result();
rst.setRETCODE("1000");
rst.setRETMSG("fail");
return rst;
}
}
6、创建Controller类
package com.xhjz.controller;
import org.springframework.stereotype.Controller;
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
@RequestMapping(value = "/config")
public class Config {
@ResponseBody
@RequestMapping(value = "/add1", method = RequestMethod.POST)
public Result add1(@RequestBody MarketCfg cfg) {
Result rst = Result.create();
if (cfg == null) {
rst.setRETMSG("请求数据不合法或为空!");
return rst;
}
if (cfg.getCfgId() == null || cfg.getCfgName() == null) {
rst.setRETMSG("MarketCfg为空!");
return rst;
}
String rspJson = cfg.toString();
System.out.println(rspJson);
return rst;
}
}
7、测试