Java SpringMVC:ModelAndViewContainer:View为[null];默认模型{Some Object}
我使用SpringMVC和ContentNegotingViewResolver来处理我的响应对象并解析为特定格式。当我从控制器返回对象时,作为响应,它添加我的响应对象,但也添加我用来映射请求参数的对象。我的控制器被用作rest控制器。因为我想使用spring和ContentNegotiatingViewResolver创建rest服务。调试后,我发现InvocableHandlerMethdod类中invokeForRequest方法包含以下参数:Java SpringMVC:ModelAndViewContainer:View为[null];默认模型{Some Object},java,rest,spring-mvc,spring-validator,Java,Rest,Spring Mvc,Spring Validator,我使用SpringMVC和ContentNegotingViewResolver来处理我的响应对象并解析为特定格式。当我从控制器返回对象时,作为响应,它添加我的响应对象,但也添加我用来映射请求参数的对象。我的控制器被用作rest控制器。因为我想使用spring和ContentNegotiatingViewResolver创建rest服务。调试后,我发现InvocableHandlerMethdod类中invokeForRequest方法包含以下参数: public final Object in
public final Object invokeForRequest(NativeWebRequest request, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
...............................................
在ModelAndViewContainer参数中包含以下详细信息:
ModelAndViewContainer: View is [null]; default model {oauthClientDetail=com.netsol.entities.OauthClientDetail@9ac35e, org.springframework.validation.BindingResult.oauthClientDetail=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
我无法理解为什么要在ModelAndViewContainer中设置OauthClientDetail对象,以及如何防止这种情况
以下是我的控制器代码:
@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
public 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.setErrorCode(CommonUtility.API_RESPONSE_STATUS.SUCCESS.getValue());
response.setErrorMessage(CommonUtility.API_RESPONSE_STATUS.SUCCESS.getMessage());
response.setResponse(detail);
}else{
response.setErrorCode(CommonUtility.API_RESPONSE_STATUS.FAIL.getValue());
response.setErrorMessage(CommonUtility.API_RESPONSE_STATUS.FAIL.getMessage());
response.setResponse(null);
}
return response;
}
我的回应是:
{
"oauthClientDetail": {
"userId": null,
"clientId": "my-client-with-secret",
"additionalInformation": null,
"authorities": null,
"authorizedGrantTypes": null,
"autoapprove": null,
"clientSecret": "12345678",
"clientSecretConfirm": "12345678",
"resourceIds": null,
"scope": null,
"webServerRedirectUri": null,
"createdOn": null,
"updatedOn": null,
"active": null,
"lastLogin": null
},
"apiResponse": {
"errorCode": 0,
"errorMessage": "Success",
"response": {
"userName": "my-client-with-secret",
"dateCreated": null,
"dateUpdated": null,
"active": "true"
}
}
}
作为响应,oauthClientDetail对象包含我随请求发送并映射到该对象中的值。由此,我假设在响应中设置了map对象。如何防止在响应中设置此对象 我从这个链接中找到了解决方案。需要在控制器中添加@ResponseBody注释,并设置products={application/json}。以下是我的新代码:
@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;
}
{
"oauthClientDetail": {
"userId": null,
"clientId": "my-client-with-secret",
"additionalInformation": null,
"authorities": null,
"authorizedGrantTypes": null,
"autoapprove": null,
"clientSecret": "12345678",
"clientSecretConfirm": "12345678",
"resourceIds": null,
"scope": null,
"webServerRedirectUri": null,
"createdOn": null,
"updatedOn": null,
"active": null,
"lastLogin": null
},
"apiResponse": {
"errorCode": 0,
"errorMessage": "Success",
"response": {
"userName": "my-client-with-secret",
"dateCreated": null,
"dateUpdated": null,
"active": "true"
}
}
}
@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;
}