@Controller
@RequestMapping("/house/rent")
public class RentInfoController {
@Resource
RentInfoService rentInfoService;
Logger logger = LoggerFactory.getLogger(RentInfoController.class);
@RequestMapping("/add")
@JsonBody
/**
*添加发布信息
*/
public Object addRentInfo(RentInfo rentInfo,Model model) {
try{
rentInfo.setPostTime(new Date());
rentInfo.setUserId(1);
rentInfoService.insertRentInfo(rentInfo);
}catch (Exception e){
logger.error("添加异常",e);
return JsonResult.getErrorMessage(-1,"发布信息添加失败!");
}
model.addAttribute("id", rentInfo.getId());
return "redirect:/house/rent/select/rentinfo";
}
}
添加成功后跳转。使用了@JsonBody标签,addRentInfo()的返回值类型定义为Object。
JsonResult类的定义:
/*生成Json串*/
public class JsonResult {
/**
* 返回错误状态的json串
*
* @param status 错误代码,非零数值
* @param message 错误消息
* @return CodeMessage
*/
public static CodeMessage getErrorMessage(final int status, final String message) {
return new CodeMessage() {
@Override
public int getStatus() {
return status;
}
@Override
public String getMessage() {
return message;
}
@Override
public Object getData() {
return null;
}
};
}
/**
* 返回成功状态的json串
*
* @param data 成功时返回的数据
* @return CodeMessage
*/
public static CodeMessage getSuccessMessage(final Object data) {
return new CodeMessage() {
@Override
public int getStatus() {
return 0;
}
@Override
public String getMessage() {
return null;
}
@Override
public Object getData() {
return data;
}
};
}
}
接口CodeMessage的定义:
public interface CodeMessage {
int OK = 0;
int SYSTEM_ERROR = -1;
int getStatus();
java.lang.String getMessage();
java.lang.Object getData();
}
说明:之所以定义CodeMessage接口是为了统一一个公司的标准;JsonResult中定义了两个静态方法:getErrorMessage()和getSuccessMessage()来返回统一格式的JsonResult。JsonBody是公司自定义的一个标准,用@JsonBody注解后,方法返回值默认为CodeMessage接口的实现。
测试:
@Controller
@RequestMapping("/test")
public class TestController {
static final Logger logger = LoggerFactory.getLogger(UserController.class);
@RequestMapping(value = "/json")
@JsonBody
public Object findPassword() {
logger.info("=======================================");
User user = new User();
user.setName("tom");
return JsonResult.getErrorMessage(1,"user");
}
}
return JsonResult.getErrorMessage(1,"user");
返回的Json格式为:{"status":1,"message":"user","data":null}
return JsonResult.getSuccessMessage(user);
返回的Json格式为:
{"status":0,"message":null,"data":{"id":null,"username":null,"password":null,"token":null,"mac":null,"name":"tom","createTime":null,"lastLogin":null,"phone":null,"gender":null,"dept":null,"qq":null,"rtx":null,"coEmail":null,"userEmail":null,"hometown":null,"hobby":null,"headPhoto":null,"houseStatus":null,"area":null,"address":null,"desc":null,"status":null}}
return user;
返回的Json格式为:
{"status":0,"message":null,"data":{"id":null,"username":null,"password":null,"token":null,"mac":null,"name":"tom","createTime":null,"lastLogin":null,"phone":null,"gender":null,"dept":null,"qq":null,"rtx":null,"coEmail":null,"userEmail":null,"hometown":null,"hobby":null,"headPhoto":null,"houseStatus":null,"area":null,"address":null,"desc":null,"status":null}}
当我们把JsonBody的注解去掉后,系统将按照web.xml中配置的那样去查找相关的jsp页面:
HTTP Status 404 - /WEB-INF/jsp/test/json.jsp
采用ResponseBody注解后:
return user;
返回json数据格式:{"id":null,"username":null,"password":null,"token":null,"mac":null,"name":"tom","createTime":null,"lastLogin":null,"phone":null,"gender":null,"dept":null,"qq":null,"rtx":null,"coEmail":null,"userEmail":null,"hometown":null,"hobby":null,"headPhoto":null,"houseStatus":null,"area":null,"address":null,"desc":null,"status":null}
执行:
return JsonResult.getSuccessMessage(user);
返回格式为:
{"message":null,"data":{"id":null,"username":null,"password":null,"token":null,"mac":null,"name":"tom","createTime":null,"lastLogin":null,"phone":null,"gender":null,"dept":null,"qq":null,"rtx":null,"coEmail":null,"userEmail":null,"hometown":null,"hobby":null,"headPhoto":null,"houseStatus":null,"area":null,"address":null,"desc":null,"status":null},"status":0}
我们发现:其实就是返回一个普通的CodeMessage对象。如果使用@JsonBody,则返回另一种统一规范的json对象。