我们在本示例中,使用Spring3.0 MVC, Spring 3.0 MVC已经支持JSON了。
这里我们使用基于注解的方式,也是springMVC 3.0所支持的RESTFul风格的。
1.首先加入两个jar依赖,这里以maven构建为例:
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.8.4</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.8.4</version>
- </dependency>
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.4</version> </dependency>
这是spring MVC处理json数据时,所必须的jar依赖。
2.在spring mvc的配置文件中加入配置
这里我们使用的是spring mvc的注解,如例:
- <context:component-scan base-package="com.hundsun.twioo.business.action" />
- <bean class="org.springframework.web.servlet.mvc.annotation.
- DefaultAnnotationHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.annotation.
- AnnotationMethodHandlerAdapter" >
- <property name="messageConverters">
- <util:list id="beanList">
- <ref bean="mappingJacksonHttpMessageConverter"/>
- </util:list>
- </property>
- </bean>
- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.
- http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <context:annotation-config/>
<context:component-scan base-package="com.hundsun.twioo.business.action" /> <bean class="org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <util:list id="beanList"> <ref bean="mappingJacksonHttpMessageConverter"/> </util:list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework. http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <context:annotation-config/>
注意:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter的Bean配置中,我们加入了messageConverters属性,在该属性中我们有配置mappingJacksonHttpMessageConverter这个Bean,它就是用来处理json数据转换的。
在mappingJacksonHttpMessageConverter的Bean配置中,我们有一个supportedMediaTypes属性,在这个属性
中我们添加了text/html;charset=UTF-8这个值,它是为了处理返回的json数据的编码,默认是ISO-88859-1的,这里我们把它设置为UTF-8,如果有乱码的情况,我们只需要修改这里就可以了。
3.控制器Controller上的注解使用
如例:
- @Controller
- @RequestMapping("/logins")
- public class LoginController {
- @Autowired
- private LoginManagerService loginManagerService;
- @Autowired
- private PermitManagerService permitManagerService;
- private Logger log = Logger.getLogger(LoginController.class);
- /**
- * 用户登录系统
- * @return
- */
- @RequestMapping(value="/login")
- public ModelAndView login(HttpServletRequest request,HttpSession session){
- User user = null;
- List<Module> moduleList = null;
- try {
- //TwiooID
- Integer twiooId = ServletRequestUtils.getIntParameter(request, "twiooId");
- //群ID
- Integer groupId = ServletRequestUtils.getIntParameter(request, "groupId");
- user = loginManagerService.login(twiooId, 3);
- if(null != user){
- moduleList = permitManagerService.findPermit(user.getId());
- }
- session.setAttribute("user", user);
- session.setAttribute("permit", moduleList);
- session.setAttribute("twiooId", twiooId);
- session.setAttribute("groupId", groupId);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("LoginController.login() execute error!!!");
- }
- System.out.println("login...................");
- return new ModelAndView("index");
- }
- /**
- * 用户退出系统
- * @return
- */
- @RequestMapping(value="/exit")
- public ModelAndView exit(HttpServletRequest request,HttpSession session){
- session.removeAttribute("user");
- session.removeAttribute("permit");
- session.removeAttribute("twiooId");
- session.removeAttribute("twiooId");
- System.out.println("exit....................");
- return new ModelAndView("index");
- }
- /**
- * 测试返回JSON数据
- * @param session
- * @return
- */
- @RequestMapping(value="/test")
- @ResponseBody
- public Object test(HttpSession session){
- System.out.println("test....................");
- return session.getAttribute("permit");
- }
- public LoginManagerService getLoginManagerService() {
- return loginManagerService;
- }
- public void setLoginManagerService(LoginManagerService loginManagerService) {
- this.loginManagerService = loginManagerService;
- }
- public PermitManagerService getPermitManagerService() {
- return permitManagerService;
- }
- public void setPermitManagerService(PermitManagerService permitManagerService) {
- this.permitManagerService = permitManagerService;
- }
- }
@Controller
@RequestMapping("/logins")
public class LoginController {
@Autowired
private LoginManagerService loginManagerService;
@Autowired
private PermitManagerService permitManagerService;
private Logger log = Logger.getLogger(LoginController.class);
/**
* 用户登录系统
* @return
*/
@RequestMapping(value="/login")
public ModelAndView login(HttpServletRequest request,HttpSession session){
User user = null;
List<Module> moduleList = null;
try {
//TwiooID
Integer twiooId = ServletRequestUtils.getIntParameter(request, "twiooId");
//群ID
Integer groupId = ServletRequestUtils.getIntParameter(request, "groupId");
user = loginManagerService.login(twiooId, 3);
if(null != user){
moduleList = permitManagerService.findPermit(user.getId());
}
session.setAttribute("user", user);
session.setAttribute("permit", moduleList);
session.setAttribute("twiooId", twiooId);
session.setAttribute("groupId", groupId);
} catch (Exception e) {
e.printStackTrace();
log.error("LoginController.login() execute error!!!");
}
System.out.println("login...................");
return new ModelAndView("index");
}
/**
* 用户退出系统
* @return
*/
@RequestMapping(value="/exit")
public ModelAndView exit(HttpServletRequest request,HttpSession session){
session.removeAttribute("user");
session.removeAttribute("permit");
session.removeAttribute("twiooId");
session.removeAttribute("twiooId");
System.out.println("exit....................");
return new ModelAndView("index");
}
/**
* 测试返回JSON数据
* @param session
* @return
*/
@RequestMapping(value="/test")
@ResponseBody
public Object test(HttpSession session){
System.out.println("test....................");
return session.getAttribute("permit");
}
public LoginManagerService getLoginManagerService() {
return loginManagerService;
}
public void setLoginManagerService(LoginManagerService loginManagerService) {
this.loginManagerService = loginManagerService;
}
public PermitManagerService getPermitManagerService() {
return permitManagerService;
}
public void setPermitManagerService(PermitManagerService permitManagerService) {
this.permitManagerService = permitManagerService;
}
}
注意:test()方法上,我们使用了一个@ResponseBody的注解,Spring3.0 MVC @ResponseBody的作用是把返回值直接写到HTTP response body里。 test()这个方法就是我们这里关注的地方,它就是返回json数据到客户端去的。还要注意,这个test()方法返回的是Object(这里可以是任意类型),而不ModelAndView。