我想通过control类返回给前端json字符串,但是现在是每个接口返回的格式都不相同。
我想写一个Response类,返回
{
status:true,
message: "操作失败,取错误信息",
body:”操作成功,取数据“
}
但是不懂这样的Response类怎么写。body里面可能放字符串,可能放各种model类。
伊谢尔伦2017-04-18 09:48:22
Use template classes
public class Response<T> {
private boolean status;
private String message;
private T body;
...
}
When to use:
Response response = new Response<String>();
But personally I don’t like this approach very much. It is recommended that when it succeeds, the response returns the entity you want to return; when it fails, it returns an error entity. As for the return value, just use http status directly. This is closer to RESTful
PHP中文网2017-04-18 09:48:22
Put a base class and put the common attributes into this base class, and then other responses can inherit this class
PHP中文网2017-04-18 09:48:22
Actually, I think this requirement should be met with HttpServletResponseWrapper
this thing. I don’t know if you can understand it now