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"
}
}