java - 如何在requestMapping下自由的返回数据?
大家讲道理
大家讲道理 2017-04-18 09:34:00
0
3
294
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
左手右手慢动作

Here you can customize an ApiResult class as the carrier of the return value,

public class ApiResult<T> implements Serializable {

    private int status;        //返回状态,0标示正确返回,-1表示错误返回,
    private String errmsg;    //返回的错误信息
    private T data;            //返回的数据集合
    /**
    * get,set方法,相关的静态方法
    */
}
@RequestMapping("/firstapi")
@ResponseBody
public ApiResult firstApi(String key,String style,String type){
    if(aboutKey.equal(key)){
        List<Model> models =  aboutModel.queryModels(style, type);
        return ApiResult.succ(models);
    }
    return ApiResult.fail("错误原因");
}

{

"status": 0,
"message": "成功",
"data": {
    ...
}

}

{
    "status": -1,
    "message": "失败描述",
    "data": {
        ...
    }
}
阿神

You can use modelAndView, but it is recommended that the judgment logic be placed in the service layer, so that the execution result is returned directly and the success is judged on the front end. There is no need to judge whether to jump to the success interface in the background

PHPzhong

You have to think clearly whether this method needs to return data or jump to a page. I think the suggestion above is OK. Put the data into the model and then return the page

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