restful 风格处理响应
现在的项目处理已经越来越流行
restful 风格处理响应,里面 包含 code message,data
现在写一个建的restfulmessage
package Util; /** * Created by Guozhijie on 2016/8/29. */ public class RESTfulMessage { public static final String SECCESS="succes"; public static final String ERROR="error"; private Integer code; private String message; private Object data; public static class MessageBuilder{ private Integer code; private String messgae; private Object data; public MessageBuilder(Integer code){ this.code=code; } public MessageBuilder(Integer code,String messgae){ this.code=code; this.messgae=messgae; } public RESTfulMessage build(){ return new RESTfulMessage(this); } public RESTfulMessage.MessageBuilder message(String messgae){ this.messgae=messgae; return this; } public RESTfulMessage.MessageBuilder data(Object data){ this.data=data; return this; } } private static RESTfulMessage.MessageBuilder messageCode(Integer code){ return new RESTfulMessage.MessageBuilder(code); } private static RESTfulMessage.MessageBuilder messageCode(Integer code,String messgae){ return new RESTfulMessage.MessageBuilder(code,messgae); } public static RESTfulMessage.MessageBuilder success(){ return messageCode(Integer.valueOf(1),"success"); } public static RESTfulMessage.MessageBuilder error(){ return messageCode(Integer.valueOf(0),"error"); } public RESTfulMessage(RESTfulMessage.MessageBuilder builder){ if(builder.code!=null){ this.code=builder.code;; } if(builder.messgae!=null){ this.message=builder.messgae; } if(builder.data!=null){ this.data=data; } } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
在spring-web 为4.2.0 的时候就可以使用
例如
package com.wonder; import Util.RESTfulMessage; import org.apache.log4j.Logger; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.apache.log4j.*; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by Guozhijie on 2016/9/10. */ @Controller @RequestMapping("app/user") public class LoginController { private static Logger logger=Logger.getLogger(LoginController.class); @RequestMapping("/login/{username}/{pwd}") public ResponseEntity<RESTfulMessage> login(@PathVariable("username") String username){ return ResponseEntity.ok().body(RESTfulMessage.success().data(true).message("登录成功").build()); } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25