为什么响应实体返回带有setter / getter名称而不是datamember名称的json

时间:2015-03-05 05:34:50

标签: java json spring

在Spring ResponseEntity返回的下面的代码中,我得到一个带有setter名称而不是数据成员名称的json。 当我将setData更改为setResult并将getData更改为getResult时。响应更改为结果。 不确定它属于哪个类别,无论是java还是spring。标记两者。

POJO:

public class ResponseObject {

private Object result;
private String message;

public void prepareResponse(Object result,HttpStatus status,String message){
    this.result=result;
    this.httpStatus=status;
    this.message=message;
}
public Object getData() {//gettername is data here
    return result;
}
public void setData(Object result) {//settername is data here
    this.result = result;
}


@Override
public String toString() {
    return "ResponseObject [result=" + result + ",  httpStatus="
            + httpStatus + ", message=" + message + "]";
}

控制器

@RequestMapping(value = "currentuser", method = RequestMethod.GET,produces="application/json")
    public ResponseEntity<ResponseObject> getUserInfo(HttpServletRequest requestServlet,HttpServletResponse responseServlet, HttpSession session) {
        String supervisor = null;
        HashMap loginMap    =   new HashMap();
        ResponseObject  resObj  =   new ResponseObject();
        try {
                supervisor = SecurityContextHolder.getContext().getAuthentication().getName();
                loginMap.put("userName", supervisor);

                System.out.println("username >>>>" + loginMap);
                if(supervisor.contains("anonymous")){
                    /*responseServlet.setStatus(responseServlet.SC_UNAUTHORIZED);*/
                    resObj.setHttpStatus(HttpStatus.UNAUTHORIZED);
                }
                resObj.setData(loginMap);
        } catch (Exception exception) {
            LogUtil.fatalException(CLASS_NAME, "createProjectScreen",
                    exception.getMessage() + " :::InputParams ==>", exception,
                    LOG_NAME);
            resObj.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
        }  
        return new ResponseEntity<ResponseObject>(resObj,resObj.getHttpStatus());
    }

响应:

{
httpStatus: "UNAUTHORIZED",
message: null,
data: {
userName: "anonymousUser"
}
}

1 个答案:

答案 0 :(得分:1)

以下作品

  

默认机制是根据Bean约定使用隐含名称 -   对于getter和setter,首先是“get”或“set”,小写   char - 但这可以通过注释覆盖   (@JsonSetter / @JsonGetter for Jackson 1.0,或@JsonProperty   杰克逊1.1)。