简体   繁体   中英

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. 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;charset=utf-8 Content-Language: en Content-Length: 1097 Date: Wed, 16 Nov 2016 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.

Adding my DTO definition if any problems here.. 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,

Also please look documentation : 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.

<!-- 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...

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 -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

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.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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