0

In the below code when returned by the Spring ResponseEntity i am getting a json with the setter name and not the data member name. When i change the setData to setResult and getData to getResult. The response is changed to result. Not sure under which category it falls under, either java or springs. tagging both.

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 + "]";
}

Controller

@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());
    }

Response:

{
httpStatus: "UNAUTHORIZED",
message: null,
data: {
userName: "anonymousUser"
}
}
rolling stone
  • 543
  • 1
  • 8
  • 26

1 Answers1

2

The below works

The default mechanism is to use implied name as per Bean convention – for getters and setters leave out "get" or "set", lower case first char – but this can be overridden by annotations (@JsonSetter/@JsonGetter for Jackson 1.0, alternatively @JsonProperty for Jackson 1.1).

Paul John
  • 1,626
  • 1
  • 13
  • 15
  • There is not specific reason to have a different. But why it is returning based on the getter method name ? – rolling stone Mar 05 '15 at 11:09
  • I edited my question with info from jackson documentation - By default Jackson use the getter and setter to serialize and deserialize.. Hope this helps, thanks. – Paul John Mar 05 '15 at 11:19
  • (No need to sign your answer, and iirc is actively discouraged in the faq.) – Dave Newton Mar 05 '15 at 11:51
  • I will follow that from now on. Thank you! – Paul John Mar 05 '15 at 11:53
  • @Paul but then why in general for toString method we are using the datamamber and not getters/setters ? – rolling stone Mar 07 '15 at 15:34
  • @sridhar,i am not sure if this helps..but as far as I know, I think it doesnt hurt at all to use getters in the toString() method. I did a quick scan on using getters in toString method this and came across this post too...http://stackoverflow.com/questions/2073159/is-it-better-use-getter-methods-or-to-access-private-fields-directly-when-overri – Paul John Mar 07 '15 at 15:51