服务详情:
End point Url: Some url
Request Body: {"userIds":["someID123456789"]}
Header: application/json
SpringConfig.xml
<bean id="rumbaDeleteUserClient" class="com.app.services.UserServiceImpl">
<property name="deleteUserTemplate" ref="deleteUserTemplate"/>
</bean>
<bean id="deleteUserTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
<ref bean="stringConverter"/>
<ref bean="encodedType"/>
<ref bean="marshallingConverter"/>
</list>
</property>
<property name="requestFactory" ref="deleteUserRequestFactory"></property>
</bean>
<bean id="deleteUserRequestFactory" class= "org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<property name="readTimeout" value="${connection.read.timeout}" />
<property name="connectTimeout" value="${connection.timeout}" />
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="*"/>
</bean>
<bean id="marshallingConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="ulcServiceMarshaller"/>
<constructor-arg ref="olcServiceMarshaller"/>
<property name="supportedMediaTypes" value="application/xml" />
</bean>
<bean id="encodedType"
class="org.springframework.http.converter.FormHttpMessageConverter">
<property name="supportedMediaTypes" value="*"/>
</bean>
<bean id="ulcServiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.pearson.psn.rumba.model" />
</bean>
<bean id="olcServiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.pearson.psn.rumba.org.model" />
</bean>
UserServiceImpl.java
private RestTemplate deleteUserTemplate;
public void setDeleteUserTemplate(RestTemplate deleteUserTemplate) {
this.deleteUserTemplate = deleteUserTemplate;
}
public void method1() {
String uri = "some end point url";
HttpComponentsClientHttpRequestFactory factroy = (HttpComponentsClientHttpRequestFactory) deleteUserTemplate
.getRequestFactory();
deleteUserTemplate.setRequestFactory(factroy);
String[] userArray = new String[1];
userArray[0] = "someID123456789";
// create request body
JSONObject request = new JSONObject();
request.put("userIds", userArray);
// body
HttpEntity<String> requestEntity = new HttpEntity<String>(request.toString());
ResponseEntity<String> response = deleteUserTemplate.postForEntity(uri,
requestEntity, String.class);
}
作为一个POST类型的服务,我也尝试使用postForObject和交换方法,它不起作用。我想我通过请求机构的方式是错误的。那么如何像{“userIds”那样传递请求体:[“someID123456789”]}
任何人都可以提出你的想法。提前谢谢。