java api 返回统一格式的的json字符串
ringa_lee
ringa_lee 2017-04-18 09:46:22
0
3
853

我想通过control类返回给前端json字符串,但是现在是每个接口返回的格式都不相同。
我想写一个Response类,返回

{
    status:true,
    message: "操作失败,取错误信息",
    body:”操作成功,取数据“
 }

但是不懂这样的Response类怎么写。body里面可能放字符串,可能放各种model类。

ringa_lee
ringa_lee

ringa_lee

reply all(3)
伊谢尔伦

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

洪涛

Put a base class and put the common attributes into this base class, and then other responses can inherit this class

洪涛

Actually, I think this requirement should be met with HttpServletResponseWrapperthis thing. I don’t know if you can understand it now

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template