Spring MVC: ModelAndViewContainer: View is [null]; default model{Some-Object}

送分小仙女□ 提交于 2019-12-02 11:58:06
Harmeet Singh Taara

I found the Solution from this MappingJacksonJsonView return top-level json object link. Need to add @ResponseBody annotation in controller and set produces={"application/json"}. Follwoing is my new code:

 @RequestMapping(value = "/changePassword", method = RequestMethod.POST, produces={"application/json"})
public @ResponseBody ApiResponse<UserDetail> changePassword(@Valid OauthClientDetail clientDetail,
        BindingResult bindingResult,
        @RequestParam(value = "password", required = true) String password) {
    logger.info("In changePassword Service...... ");

    if(bindingResult.hasErrors()){
        throw new InvalidRequestException("Error", bindingResult);
    }

    UserDetail detail = userService.changeUserPassword(password, clientDetail, encoder);
    ApiResponse<UserDetail> response = new ApiResponse<UserDetail>();
    if(detail != null){
        response.setSuccess(CommonUtility.API_RESPONSE_STATUS.SUCCESS.getValue());
        response.setMessage(CommonUtility.API_RESPONSE_STATUS.SUCCESS.getMessage());
        response.setResponse(detail);
    }else{
        response.setSuccess(CommonUtility.API_RESPONSE_STATUS.FAIL.getValue());
        response.setMessage(CommonUtility.API_RESPONSE_STATUS.FAIL.getMessage());
        response.setResponse(null);
    }
    return response; 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!