0

I am trying to make a POST call using fiddler to a spring rest API,

@RequestMapping(value = "/GetPlanByBasicContext/", method = RequestMethod.POST)
public @ResponseBody TravelPlan getPlanByBasicContext(@RequestBody BasicPlanContext b) {
    return planService.getPlan(b));
}

Request on fiddler:

http://localhost:8080/now/travelPlan/GetPlanByBasicContext/

Header:

User-Agent: Fiddler
Host: localhost:8080
Content-Length: 248

POST payload:

{
    "sourceLocation": "",
    "destinationLocation": "",
    "modeOfTransport": "car", 
    "travellers": {
        "age1to16": 0,
        "age17to30": 0,
        "age31to50": 0,
        "age50plus": 0
    },
    "dates": {
        "startDate": "",
        "endDate": ""
    }
}

The attributes in payload are same as that in class BasicPlanContext, along with getter and setter.

I get following error:

 415 Unsupported Media Type
 The server refused this request because the request entity is in a format not supported 
 by the requested resource for the requested method.

Tried replacing @RequestBody with @ModelAttribute, which didnt help.

Also I have following libs:

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

EDIT 1: Tried adding following to the header,

Content-Type: application/json

And following to the POST method,

@RequestMapping(..., headers="Accept=application/json")

This results in 400 The request sent by the client was syntactically incorrect.

varsh
  • 152
  • 10

1 Answers1

0

You are trying to send a JSON payload but the server rejects the request with status 415 which indicates that the media type of the request causes problems.

First you need to define that the request method accepts JSON payloads

@RequestMapping(..., headers="Accept=application/json")

and the fiddler request should include the header

Content-Type: application/json
wero
  • 32,544
  • 3
  • 59
  • 84
  • I have tried this earlier and this throws, 400 Bad Requst, "The request sent by the client was syntactically incorrect." – varsh Apr 08 '16 at 13:08
  • 1
    @varsh A reason for the 400 would be that the JSON could not be parsed into a `BasicPlanContext` object. Do you have an error traces in the server log? – wero Apr 08 '16 at 13:13
  • I dont see any logs on the server console, other than SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". – varsh Apr 08 '16 at 13:23
  • 1
    @varsh http://stackoverflow.com/questions/17326976/spring-rest-using-jackson-400-bad-request-logging – wero Apr 08 '16 at 13:25
  • thanks a lot, I got the logs, which says "JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object". Following link helped resolving the issue which was with inner class: http://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class – varsh Apr 08 '16 at 14:10