Spring MVC:ModelAndViewContainer:View是; 默认模型{Some-Object}
我使用带有ContentNegotiatingViewResolver
Spring-mvc来处理我的响应对象并解析成特定的格式。 当我从控制器返回对象时,作为响应它添加我的响应对象,但也添加我用于映射请求参数的对象。 我的控制器充当rest控制器。 因为我想使用带有ContentNegotiatingViewResolver
spring创建rest-services。 调试之后,我找到了InvocableHandlerMethdod
类,其中invokeForRequest
方法包含以下参数:
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}
我无法OauthClientDetail
,为什么在ModelAndViewContainer
设置OauthClientDetail
对象以及我如何防止这种情况。
Follwing是我的控制器代码:
@RequestMapping(value = "/changePassword", method = RequestMethod.POST) public ApiResponse 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 response = new ApiResponse(); 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; }
rest-servlet.xml配置:
bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> java.util.List java.lang.String java.util.Map java.lang.Object java.util.Set java.lang.Long java.util.Date
我对chrome postman
客户的要求:
POST /empirecl-api/api/changePassword HTTP/1.1 Host: localhost:8080 Authorization: Bearer b3f46274-b019-4d7b-a3bd-5c19e9660c2f Cache-Control: no-cache Content-Type: application/x-www-form-urlencoded clientId=my-client-with-secret&clientSecret=12345678&clientSecretConfirm=12345678&password=12345678
我的回答是:
{ "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
对象包含值,我随请求发送并映射到对象中。 由此,我假设,我的地图对象在响应中设置。 我如何阻止此对象在响应中设置。
我发现此MappingJacksonJsonView的解决方案返回顶级json对象链接。 需要在控制器中添加@ResponseBody
注释并设置@ResponseBody
produces={"application/json"}
。 Follwoing是我的新代码:
@RequestMapping(value = "/changePassword", method = RequestMethod.POST, produces={"application/json"}) public @ResponseBody ApiResponse 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 response = new ApiResponse (); 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; }