原始servlet向浏览器回写数据
@RequestMapping(value = "/save5",method = RequestMethod.GET)
public void save5(HttpServletResponse response) throws IOException
{
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("哈哈");
}
@RequestMapping(value = "/save7",produces = "text/html; charset=utf-8")
@ResponseBody //告知SpringMVC 不进行视图跳转 直接进行数据回写
public String save7()
{
return "hello world";
}
2.使用json的转换工具将对象转化成json格式字符串再返回
1.首先导入坐标
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
2.
@RequestMapping(value = "/save8")
@ResponseBody //告知SpringMVC 不进行视图跳转 直接进行数据回写
public String save8() throws JsonProcessingException {
User user = new User("haha",18);
//使用json的转换工具 将对象转换成json格式字符串 再返回
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;
}
@RequestMapping(value = "/save9")
@ResponseBody //告知SpringMVC 不进行视图跳转 直接进行数据回写
public User save9() throws JsonProcessingException
{
User user = new User("haha",18);
//期望springMVC自动将user转换成json格式的字符串
return user;
}
但是springMVC没有这么智能,需要指定适配器帮你转换
在DispatcherServlet.properties文件有一个AnnotationMethodHandlerAdapter类
需要指定该类使用json格式帮你自动转化
该类有一个HttpMessageConverter方法,指定该方法使用json格式帮你自动转化
内部需要HttpMessageConverter对象,为list类型,使得该对象是MappingJackson2HttpMessageConverter的转换格式
<!--配置处理器映射器 -->
<bean id="AnnotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
现在就可以啦
@RequestMapping(value = "/save9")
@ResponseBody //告知SpringMVC 不进行视图跳转 直接进行数据回写
public User save9() throws JsonProcessingException
{
User user = new User("haha",18);
//期望springMVC自动将user转换成json格式的字符串
return user;
}
但是,也可以不用这么麻烦就能配置
需要在spring-mvc.xml加入命名空间和命名空间的地址location
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
也就是这样
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
最后,在spring-mvc.xml加入
<mvc:annotation-driven></mvc:annotation-driven>
即可