使用Spring RESTful服务接收和返回JSON最佳实践
来源:互联网 发布:西门子300plc编程实例 编辑:程序博客网 时间:2024/06/05 16:24
首先要导入相应的架包
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.7</version>
</dependency>
然后再
<!-- 添加的两个 自动配置器(restful 风格所必须) -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
以下为前端的相关ajax请求
function sendNews()
{
var url="${pageContext.request.contextPath}/NewsAction/addNews";
var title = "title="+$('#title').val();
var author = "author="+$('#author').val();
var content = "content="+UE.getEditor('container').getContent();
var dataText = title+"&"+author+"&"+content;
alert("dataText: "+dataText);
$.ajax({
url:url,
dataType:"text", /* 后端 返回值类型 为 String 时 此处 必须指定 dataType 为 text */
data:dataText,
type:"post",
success:function(rspText)
{
alert("哈哈哈哈");
alert("rspText: "+rspText);
}
});
}
以下为具体的实现类
@RequestMapping(value="addNews",produces="application/json;charset=utf-8")
@ResponseBody
public News addNews(News news)
{
System.out.println("--------- 添加 news ---------");
news.setId(UUID.randomUUID().toString());
news.setNewsdate(new Date().toString());
String newsStr = "";
try
{
newsStr = new ObjectMapper().writeValueAsString(news);
System.out.println("newsStr:\t"+newsStr);
}
catch (JsonProcessingException e) {
e.printStackTrace();
}
String a = "{'id':'fae9acde-2808-4153-ae50-c15f7b7b94b6','title':null,'content':null,'newsdate':'Tue Apr 11 12:20:39 CST 2017','author':null}";;
return news;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
一、实验环境的搭建
1、Spring mvc jar。
导入spring mvc运行所需jar包。导入如下(有多余)
2、json的支持jar
3、加入jQuery。
选用jquery-3.0.0.min.js,放在WebRoot/JS文件夹
导入jQuery到jsp页面如下
4、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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springmvcjson</display-name>
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</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>
5、springmvc.xml
classpath下
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- <bean name="/test01.action" class="com.xzw.json.controller.JsonTest"></bean> -->
<!-- View -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- 注解映射和适配器 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 组件扫描 -->
<context:component-scan base-package="com.xzw.json.controller"></context:component-scan>
<!-- 使用@Autowired、@Required等注解
如不必设置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>和
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>等等
-->
<context:annotation-config />
</beans>
二、实验例子编写
1、请求和返回都是JSON
a).程序发起
index.jsp的一个按钮
b).js函数
function requestByJson() {
$.ajax({
type : 'post',
url : '${pageContext.request.contextPath}/jsonsource.action',
//设置contentType类型为json
contentType : 'application/json;charset=utf-8',
//json数据
data : '{"username":"reader001","password":"psw001"}',
//请求成功后的回调函数
success : function(data) {
alert(data.username);
}
});
}
c).Controller/Mapping
@RequestMapping("/jsonsource")
//@RequestBody 将json对象转成java对象
//@ResponseBody 表示返回的是json对象
public @ResponseBody User jsonSource(@RequestBody User user){
return user;
}
- 使用Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- Spring RESTful服务接收和返回JSON最佳实践
- RESTful JSON Web服务最佳实践
- Ajax 调用restful服务返回json
- 使用JSON和Jersey的Java RESTful Web服务
- CXF+Spring+JAXB+Json构建Restful服务
- RESTful服务最佳实践——(一)
- RESTful服务最佳实践——(二)
- RESTful服务最佳实践——(三)
- RESTful服务最佳实践——(四)
- RESTful服务最佳实践——(五)
- RESTful服务最佳实践——(十)
- RESTful服务最佳实践——(十一)
- RESTful服务最佳实践——(十二)
- 【1】一铭操作系统初体验,安装jav…
- Yii 控制器学习——内容摘自http://www.yiichina.com/中文官网
- mave 启动报错 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
- 【2】一铭操作系统初体验,gcc和编…
- Yii控制器生命周期及最佳实践
- 使用Spring RESTful服务接收和返回JSON最佳实践
- 【3】一铭操作系统初体验,安装ope…
- Yii Object对象与属性相关的方法
- java.lang.IllegalArgumentException: Request header is too large
- 【4】一铭操作系统初体验,webstor…
- Yii Object和Component
- OpenGL+freeglut使用glutGameModeS…
- Yii Object的配置方法
- 【5】一铭操作系统初体验,更新fir…