@ResponseBody: 注解用于将Controller的方法返回的对象,通过springMVC 提供的HttMessageConverter 接口转换为指定格式的数据:JSON格式,通过Response响应给客户端
package com.zzxtit.springboot.mvc.ws.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zzxtit.springboot.mvc.user.entity.SysUser;
import com.zzxtit.springboot.mvc.user.service.UserService;
@Controller
@RequestMapping("ws")
public class WebServiceController {
@Autowired
private UserService userService;
@RequestMapping("userWsList")
@ResponseBody
public SysUser userList() {
return userService.getSUList().get(0);
}
}
用户界面为:
需要在应用上下文中配置转换器,在applicationContext-mvc.xml添加<mvc:annotation-driven></mvc:annotation-driven>