我正在尝试执行 RESTful Web 服务,但是,当我发送请求时,发生 HttpClientErrorException 并显示“415 Unsupported Media Type”作为消息。
这是服务调用的代码:
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
request.add(val1, "xxxxx");
request.add(val2, "************");
request.add(val3, "xxx");
request.add("type", "AUTHENTICATE");
String response = restTemplate.postForObject(url, request, String.class);
System.out.println(response.toString());
restTemplate 是从 applicationContext.xml 连接的,FormHttpMessageConverter 作为它的 messageConverter。
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg name="requestFactory" ref="httpClientFactory"/>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<constructor-arg ref="httpClient"/>
</bean>
<bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">
<property name="authenticationPreemptive" value="true"/>
<property name="connectionManagerClass"
value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/>
</bean>
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg ref="httpClientParams"/>
</bean>
这是发生的异常:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279)
at ph.com.smart.drawbridge.commons.diameter.DiameterClient.post(DiameterClient.java:21)
有任何想法吗?