流程:
客户端-----》请求json
@RequestBody将json串转成java对象
或者@RequestBody将java串转成json对象
但如果请求的是key/value,不是json,那就不需要@RequestBody将json串转成java对象
请求的是key/value,输出的是json串比较常用。
springmvc使用jackson的包进行json转换(@RequestBody和@ResposeBody使用下边的json包进行json转换)
1.springmvc.xml中配置spring内部的json转换器。
请求json串,请求方法是(,非jquery默认请求类型,故这里需要指定contentType=application/json)
jquery默认的请求类型是key-value,即(contentType=application/x-www-form-urlen)------不需要@RequestBody将json串转成java对象
映入依赖:
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
确认spring配置中配置了
<mvc:annotation-driven></mvc:annotation-driven>
jsonTest.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsonTest</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
//请求ijson,输出json
function requestjson() {
$.ajax({
type:'post',
url:'${pageContext.request.contextPath}/requestjson.action',
contentType:'application/json;charset=utf-8',
//数据格式是json串
data:'{"name":"手机","price":"99.0"}',
success:function(data) {
//返回json结果
console.log(data);
$('#request1').html(data.price+','+data.name)
}
});
}
//请求key/value
function responsejson() {
$.ajax({
type:'post',
url:'${pageContext.request.contextPath}/responsejson.action',
//提交默认类型(key,value),故再次省略 contentType:'application/json;charset=utf-8',
//数据格式是json串
data:'name=手机&&price=99.0',
success:function(data) {
//返回json结果
console.log(data);
$('#respose1').html(data.price+','+data.name)
}
});
}
</script>
</head>
<body>
<input type="button" onclick="requestjson()" value="请求ijson,输出json">
<p id="request1"></p>
<input type="button" onclick="responsejson()" value="请求key/value">
<p id="respose1"></p>
</body>
</html>
JsonTestControlller:
@Controller
public class JsonTestController {
//请求并输出json串
@RequestMapping("/requestjson")
public @ResponseBody ItemsCustom requestjson(@RequestBody ItemsCustom itemCustom){
return itemCustom;
}
@RequestMapping("/responsejson")
public @ResponseBody ItemsCustom responsejson(ItemsCustom itemCustom){
return itemCustom;
}
}
restful风格:
非resutful:: http://.../queryItems.action?id=001&type=T01
restful: http://..../items/001
将参数通过url传到服务器
代码:
web.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"
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>Archetype Created Web Application</display-name>
<!-- 将mapper,service,controller加载到spring容器(即所对应的各层整合的配置文件加载到容器),建议使用通配符批量加载,
实现方式是通过 context-param+listener(spring的全局监听器)二者结合起来综合实现的,必须要配套使用-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器springmvc-->
<!-- 这是解决post提交乱码问题的解决方案-->
<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>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置rest的前端控制器请求风格,多个控制器可以共存-->
<servlet>
<servlet-name>spring-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
ItemsController中的关键代码:
通过在方法中使用@PathVariable获取{xxx}中的{xxx}变量
@PathVariable用于将url中的模板变量映射到处理方法上
@RequestMapping("/itemsView/{id}")
public @ResponseBody
ItemsCustom itemsView(@PathVariable("id") Integer id) throws Exception {
ItemsCustom itemsCustom = itemsService.selectItemById(id);
return itemsCustom;
}