简体   繁体   English

https状态代码415甚至作为休息请求的一部分指定的内容类型

[英]https status code 415 even content-type specified as part of Rest-request

My curl request like below. 我的卷曲要求如下。

$ curl -ki -X POST -H "Content-Type:application/json;charset=utf-8" localhost:8081/Customers-Spring-MVC-Hibernate/customer -d '{"name": "anil","age": 1,"phoneNumber": 77955,"email": "pvv.anilkumar@gmail.com","password": "Password"}'

where i have given the content-type:application/json but i hit 415 status code. 我在这里给出了content-type:application / json但我打了415状态码。 Here is the response i got and i could see that Content-Type:text/html is being set is it proper here..? 这是我得到的答复,我可以看到正在设置Content-Type:text / html,这是否合适。

Content-Type: text/html;charset=utf-8 Content-Language: en Content-Length: 1097 Date: Wed, 16 Nov 2016 05:29:27 GMT 内容类型:text / html; charset = utf-8内容语言:en内容长度:1097日期:2016年11月16日,星期三05:29:27 GMT

Here is my controller definition: 这是我的控制器定义:

@RequestMapping(method = RequestMethod.POST)
public void addCustomer(
        @RequestBody final CustomerV1 customerDto) throws Exception
{

Please let me know if any workarounds.. i have tried removing and adding the charset-utf-8 but no use i still hit 415. 请让我知道是否有任何解决方法。.我尝试删除并添加charset-utf-8,但没有用,我仍然打415。

Adding my DTO definition if any problems here.. JSON DTO: 如果这里有任何问题,请添加我的DTO定义。JSON DTO:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeName("customer")
public class CustomerV1 {
    private String name;`enter code here`
    private int age;
    private long phoneNumber;
    private String email;
    private String password;

Try adding headers on your handler method as follows, 尝试如下在处理程序方法上添加标头,

 @RequestMapping(value="/add", method = RequestMethod.POST)
public void addCustomer(
        @RequestBody final CustomerV1 customerDto) throws Exception
    {
       //your code
    }

From your Github link I see that you are missing following entry in your spring-mvc-demo-servlet.xml file, 在您的Github链接中,我看到您缺少spring-mvc-demo-servlet.xml文件中的以下条目,

Also please look documentation : http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html 还请查看文档: http : //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html

With this you don't need to explicitly configure JSON as your consumption. 这样,您就不需要显式配置JSON作为您的使用对象。

<!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />

    <!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

After the Heavy analysis and brain wreck i was able to figure out what the problem is .. 经过大量分析和脑残后,我能够找出问题出在哪里..

Primarily if any one is facing the same above problem like hitting 415 error... 主要是如果有人遇到相同的上述问题,例如遇到415错误...

first check on the content-types of the rest request .. it can be one of the probable root cause for the issue.. Please have a look at the below curl request for on the content-types.. 首先检查其余请求的内容类型。这可能是导致此问题的根本原因之一。请查看以下有关内容类型的curl请求。

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d'{"name":"anil","age":2,"email":"anilmar.pvv","password":"anilkumar"}' http://localhost:8080/MySpringMvc/customer/add curl -v -H“接受:应用程序/ json” -H“内容类型:应用程序/ json” -X POST -d'{“ name”:“ anil”,“ age”:2,“ email”:“ anilmar .pvv“,” password“:” anilkumar“}' http:// localhost:8080 / MySpringMvc / customer / add

if all the content-types are proper then lies the problem with the Jackson-jars that you have downloded..my best suggestion is to remove all the jars that you have downloaded prior you face the issue 415 and try to be constant through out the versions of the Jars.. below are the version that i have made constant and major 3 dependencies that are needed for the smooth execution please ensure that you have all the 3 jars of same version ensure that those are on the class-path..then it should work fine..below is the dependency jars that i have used and came out of the maze problem. 如果所有内容类型均正确,那么这就是您下载了杰克逊罐子的问题。.我的最佳建议是在遇到问题415之前删除所有已下载的罐子,并在整个过程中保持不变。 Jars的版本..下面是我已进行了恒定的版本以及平滑执行所需的主要3个依赖项,请确保您具有相同版本的所有3个jar,确保它们位于类路径中。它应该可以正常工作..以下是我使用过的迷宫药的依赖罐。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.5</version>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM