SpringMVC学习总结(2)——SpringMVC返回json配置
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
package com.zhy.minion.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhy.minion.model.User;
import com.zhy.minion.service.UserService;
/**
* @ClassName: UserController
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Administrator
* @date 2016年4月21日 下午1:06:34
* @Copyright © 2016上海通善互联网金融信息服务有限公司
*/
@Controller
@RequestMapping({ "/user" })
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping(value = "/login")
public Object findUser(Model model) {
User user = userService.selectUserById(1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", user);
return map;
}
}