小编Mic*_*itu的帖子

Spring RestTemplate PostforObject:不支持的媒体类型

我正在尝试执行 RESTful Web 服务,但是,当我发送请求时,会出现 HttpClientErrorException,并显示“415 不支持的媒体类型”消息。

这是服务调用的代码:

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());
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这是发生的异常:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported …
Run Code Online (Sandbox Code Playgroud)

rest spring http

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

http ×1

rest ×1

spring ×1