Reputation: 3770
I have a simple Spring 4 project, based on this tutorial. I'm trying to implement a RESTful interface. Right now I'm having trouble with handling the POST request. When I try to post a JSON Object, the following error is given:
{
"timestamp":1428785473020,
"status":415,
"error":"Unsupported Media Type",
"exception":"org.springframework.web.HttpMediaTypeNotSupportedException",
"message":"Content type 'application/octet-stream' not supported",
"path":"/markers/new"
}
If I add the Content-Type header with application/json value, I have this:
{
"timestamp":1428785073247,
"status":400,
"error":"BadRequest",
"exception":"org.springframework.http.converter.HttpMessageNotReadableException",
"message":"Could not read JSON: No suitable constructor found for type
[simple type, class org.elsys.internetprogramming.trafficspy.server.Marker]:
can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.PushbackInputStream@19e19767; line: 1, column: 2];
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
No suitable constructor found for type [simple type, class
org.elsys.internetprogramming.trafficspy.server.Marker]: can not
instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.PushbackInputStream@19e19767; line: 1, column: 2]",
"path":"/markers/new"
}
Here is the code: MarkerController.java
@Controller
public class MarkerController {
private final AtomicLong id = new AtomicLong();
private Logger logger = Logger.getLogger(MarkerController.class.getName());
@RequestMapping(value="/markers", method=RequestMethod.GET)
public @ResponseBody String getMarkers(@RequestParam(value="city", defaultValue="") String city) {
logger.info("HANDLE GET REQUEST");
return "{\"id\":\"1\"}";
}
@RequestMapping(value="/markers/new", method=RequestMethod.POST)
public @ResponseBody Marker putMarker(@RequestBody Marker marker) {
logger.info("HANDLE POST REQUEST");
return marker;
}
}
Marker.java
public class Marker {
private long id;
private double longitude;
private double latitude;
private final String address;
public Marker(long id, double longitude, double latitude, String address) {
this.id = id;
this.longitude = longitude;
this.latitude = latitude;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Do you have any idea what causes this problem and how to fix it? Thank you!
Upvotes: 2
Views: 10435
Reputation: 29997
The Marker
constructor needs to be annotated with @JsonCreator
.
You need to send the Content-Type header (as in your second example), otherwise spring doesn't know that you're sending json.
Upvotes: 0
Reputation: 692121
No suitable constructor found for type [simple type, class org.elsys.internetprogramming.trafficspy.server.Marker]
Your Marker class has no default constructor. So Jackson has no way to instantiate it.
Upvotes: 1