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

后端 未结 1 702
小蘑菇
小蘑菇 2021-01-28 19:53

I am using Spring-mvc with ContentNegotiatingViewResolver for handle my response objects and parse into a specific format. When i return the object from controller,

相关标签:
1条回答
  • 2021-01-28 20:36

    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; 
    }
    
    0 讨论(0)
提交回复
热议问题