私は Spring-mvc を使用しContentNegotiatingViewResolver
て、応答オブジェクトを処理し、特定の形式に解析しています。コントローラーからオブジェクトを返すと、応答として応答オブジェクトが追加されますが、要求パラメーターをマップするために使用したオブジェクトも追加されます。私のコントローラーは残りのコントローラーとして機能します。と春を使って休息サービスを作りたいからContentNegotiatingViewResolver
です。デバッグ後、メソッドに次の引数が含まれる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
と、これを防ぐ方法を理解できません。
以下は私のコントローラーコードです:
@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;
}
rest-servlet.xml 構成:
bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="mediaType" />
<property name="ignoreAcceptHeader" value="true" />
<property name="useJaf" value="false" />
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager" />
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="html" value="text/html" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true" />
<property name="aliases">
<map>
<entry key="list" value="java.util.List" />
<entry key="string" value="java.lang.String" />
<entry key="hsahmap" value="java.util.HashMap" />
<entry key="object" value="java.lang.Object" />
<entry key="hashSet" value="java.util.HashSet" />
</map>
</property>
<property name="supportedClasses">
<list>
<value>java.util.List</value>
<value>java.lang.String</value>
<value>java.util.Map</value>
<value>java.lang.Object</value>
<value>java.util.Set</value>
<value>java.lang.Long</value>
<value>java.util.Date</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="defaultContentType" ref="htmlMediaType" />
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean id="htmlMediaType" class="org.springframework.http.MediaType">
<constructor-arg value="text" />
<constructor-arg value="html" />
</bean>
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
オブジェクトには値が含まれており、リクエストとともに送信してオブジェクトにマップします。これから、私のマップオブジェクトが応答に設定されていると思います。このオブジェクトが応答に設定されないようにする方法。