Java 415 不支持的媒体类型(org.springframework.web.client.HttpClientErrorException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42186774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
415 Unsupported Media Type (org.springframework.web.client.HttpClientErrorException)
提问by AkashSharma
I am new to WebServices. I am working on an application where I am using AnhularJs1.x at client side which sends data to Spring Rest Controller.
我是 Web 服务的新手。我正在开发一个应用程序,我在客户端使用 AnhularJs1.x 将数据发送到 Spring Rest Controller。
The architecture of the application is micro-services based. I am able to receive the data from angular to Front end Rest Controller which are in the same war.
应用程序的架构是基于微服务的。我能够从 angular 接收数据到同一场战争中的前端休息控制器。
From this controller I am calling a service which internally calls another micro-service which interacts with database.
我从这个控制器调用一个服务,该服务在内部调用另一个与数据库交互的微服务。
When I am sending the data received in my front end controller to another micro-service I get 415 Unsupported Media Type (org.springframework.web.client.HttpClientErrorException)
当我将前端控制器中接收到的数据发送到另一个微服务时,我得到415 Unsupported Media Type (org.springframework.web.client.HttpClientErrorException)
Below is my front end controller which is in the same war as angularJS
下面是我的前端控制器,它与 angularJS 处于同一场战争中
@RequestMapping(value = "/servicearticles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ServiceArticle> saveData(@RequestBody List<ServiceArticle> serviceArticleList){
System.out.println("In savedata");
System.out.println(serviceArticleList.toString());
try {
if(null != serviceArticleList && serviceArticleList.size() >0){
serviceArticleAdminService.insertData(serviceArticleList);
}else{
logger.error("File is empty. No data to save");
}
I am able to get data in this contoller : [ServiceArticle [articleId=17070, productCode=1000, productName=Business Parcel zone , zone=1], ServiceArticle [articleId=17071, productCode=1001, productName=Business Parcel zone , zone=4], ServiceArticle [articleId=17070, productCode=1012, productName=Business Parcel zone , zone=5], ServiceArticle [articleId=17070, productCode=1000, productName=Business Parcel zone , zone=1], ServiceArticle [articleId=17070, productCode=1000, productName=Business Parcel zone , zone=2]]
我能够在这个控制器中获取数据:[ServiceArticle [articleId=17070, productCode=1000, productName=Business Parcel zone , zone=1], ServiceArticle [articleId=17071, productCode=1001, productName=Business Parcel zone , zone= 4], ServiceArticle [articleId=17070, productCode=1012, productName=Business Parcel zone , zone=5], ServiceArticle [articleId=17070, productCode=1000, productName=Business Parcel zone , zone=1], ServiceArticle [articleId=17070 , productCode=1000, productName=Business Parcel zone , zone=2]]
When I call the different microservice from my serviceImpl class I get the unsupported media type error
当我从 serviceImpl 类调用不同的微服务时,我收到了不受支持的媒体类型错误
Code for serviceImpl class
serviceImpl 类的代码
private final String URI = "http://localhost:8082/admin/import/servicearticles";
@Override
public void insertData(List<ServiceArticle> serviceArticles) {
logger.error("Inside insertData() in service");
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.postForObject(URI, serviceArticles, ServiceArticle.class);
} catch (ResourceAccessException e) {
logger.error("ServiceArticleAdmin Service Unavailable.");
Below is the code for the controller in different micro-servie which maps to this call
下面是映射到这个调用的不同微服务中控制器的代码
@RequestMapping(value = "/import/servicearticles", method = RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ServiceArticle> addAll(@RequestBody List<ServiceArticle> serviceArticles) {
List<ServiceArticle> serviceArticlesAdded = serviceArticleAdminService.addAll(serviceArticles);
return new ResponseEntity(serviceArticlesAdded, HttpStatus.OK);
}
I have added the below dependencies in my pom.xml
我在 pom.xml 中添加了以下依赖项
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-dataformat-xml</artifactId>
<version>2.8.6</version>
</dependency>
I have the following bean definition in my servlet-context.xml
我的 servlet-context.xml 中有以下 bean 定义
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
<!-- To convert JSON to Object and vice versa -->
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
</bean>
Please help me figure out where am I making a mistake. Is there any way I can set response type as application/json when I am invoking restTemplate.postForObject method
请帮我弄清楚我在哪里犯了错误。当我调用 restTemplate.postForObject 方法时,有什么方法可以将响应类型设置为 application/json
I verified using a REST client plugin it works there but not through my Java code. Please help.
我使用 REST 客户端插件验证它在那里工作,但不是通过我的 Java 代码。请帮忙。
回答by Eien
It seems that "Content-Type: application/json" header is missing. Your method also works with list of articles, so the third argument in postForObject method is not correct.
似乎缺少“内容类型:应用程序/json”标头。您的方法也适用于文章列表,因此 postForObject 方法中的第三个参数不正确。
The following code should do the job:
以下代码应该可以完成这项工作:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<List<ServiceArticle>> request = new HttpEntity<>(serviceArticles, headers);
restTemplate.exchange(URI, HttpMethod.POST, request, new ParameterizedTypeReference<List<ServiceArticle>>() { });
回答by Shubham
This code may solve both of your problems:
此代码可以解决您的两个问题:
@RequestMapping(value = "/postnotewithclient",method = RequestMethod.POST)
public String postNote(@RequestBody Note notes)
{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<Note> entity = new HttpEntity<Note>(notes,headers);
return restTemplate.exchange("http://localhost:8080/api/notes", HttpMethod.POST, entity, String.class).getBody();
}