- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试执行 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)
有什么想法吗?
最佳答案
当您的客户端发送服务器不知道如何处理的媒体类型请求时,就会出现此问题。例如,您处理 url
的服务器方法可以处理 application/json
类型的请求,但您发送的是 text/xml
。服务器无法处理此类请求。为了解决这个问题,找出服务器期望的 content-type
(媒体类型),在设置 content-type 的
(将 restTemplate
对象中设置一个 header : application/jsonapplication/json
替换为服务器期望的任何内容)。此外,确保以正确的格式发送请求(如果我们谈论的是 json
,您需要具有可以将对象序列化为 的
对象)。jackson
依赖项json
关于Spring RestTemplate PostforObject : Unsupported Media Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257689/
我在 Spring rest 服务中有一个方法。 @RequestMapping(value = "test/process", method = RequestMethod.POST) public
我正在尝试使用 postforobject 检索对象,只有当我尝试检索自定义对象时,才会出现异常错误,如果我重写服务并返回单个字符串并将 postforobject 返回到一个字符串,它就可以正常工作
嗨,我希望客户端能够使用一个安静的网络服务。我使用 springboot,该服务返回一个仅包含一个对象的 JSON 列表。我想像这样使用 RestTemplate 的 postForObject Ap
给定一个类 EncoderService ,它具有以下 createNewStream 方法以及该方法中使用的一堆常量,我如何使用 mockito 来为 createNewStream 方法编写单元测
让客户端应该将纯 json 字符串发送到 RESTful 服务: ... final Gson gson = new GsonBuilder().create(); final
我必须使用 postForObject() 将 JSON 数据从一种服务方法发送到另一种服务方法方法。 我在 RestTemplate 上看到了一个例子在 this link . postForObj
我尝试模拟 Restful 服务器: private void btnPostActionPerformed(java.awt.event.ActionEvent evt) {
这个问题已经有答案了: How to parse JSON in Java (36 个回答) 已关闭 3 年前。 我想知道是否有一种方法可以操作 RestTemplate 类的方法 postForOb
我是 Mockito 以及 Spring 的 RestTemplate 的新手。我正在对一项功能进行 JUnit 测试,该功能向 Web 服务发送请求并通过使用 RestTemplate 获取响应。我
我正在尝试执行 RESTful Web 服务,但是,当我发送请求时,出现 HttpClientErrorException 消息“415 Unsupported Media Type”。 这是服务调用
我正在尝试向身份验证服务器发送帖子消息,并且我正在使用 postforobject。当我运行程序时,我收到“线程“main”中的异常 org.springframework.web.client.Ht
我试图使用 RestTemplate postForObject 使用 firebase Auth API,但是当我将 String.class 指定为其工作文件的返回类型时,但如果我指定自定义类来处
我正在尝试将消息列表发布到其余 api。如何为下面的 postJSONData 方法编写 mockito junit: public class PostDataService{ @Autow
我目前正在开发一个 Rest-Service,它需要调用同一服务器上提供的另一个 Rest-Method。因此,例如路径/myPath/foo 上的方法需要从路径/myPath/restApi/bar
使用返回 json 字符串的 restful api。格式是 { "status": "ok", "result": { } } 我正在尝试将用户配置文件的响应映射到 UserProfile
在使用 RestTemplate 发送 POST 请求时,我一直收到 400 BAD 请求。这是我的代码: MultiValueMap requestBody = new LinkedMult
我已经设置了包含字符串 ["a", "b", "c"] ,我想要 POST json 数据,例如(逗号分隔和一个字符串)这是 JSON {"view" : "a,b,c", "fruits" : "a
我目前有一个带有字符串字段的 RestTemplate 响应对象来获取响应数据。我想在同一个对象中发送 InputStream。 下面是响应类 @XmlRootElement public class
在 Spring Boot Junit 测试中,当调用 when(mockRestTemplate.postForObject(Mockito.any(), Mockito.any(),Mockito
我正在使用以下方法调用网络服务。 ResponseBean responseBean = getRestTemplate() .postForObject(url, customerBean,
我是一名优秀的程序员,十分优秀!