Spring POST请求不支持的媒体类型415

时间:2015-04-11 21:02:04

标签: java json spring spring-mvc

我有一个简单的Spring 4项目,基于this tutorial。我正在尝试实现RESTful接口。现在我在处理POST请求时遇到了麻烦。当我尝试发布JSON对象时,会出现以下错误:

    {
        "timestamp":1428785473020,
        "status":415,
        "error":"Unsupported Media Type",
        "exception":"org.springframework.web.HttpMediaTypeNotSupportedException",
        "message":"Content type 'application/octet-stream' not supported",
        "path":"/markers/new"
    }

如果我使用application / json值添加Content-Type标头,我有:

{
    "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"
}

这是代码: 的 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;
    }
}

您是否知道导致此问题的原因以及解决方法?谢谢!

2 个答案:

答案 0 :(得分:1)

  

找不到类型[simple type,class org.elsys.internetprogramming.trafficspy.server.Marker]

的合适构造函数

您的Marker类没有默认构造函数。所以杰克逊无法实例化它。

答案 1 :(得分:0)

Marker构造函数需要使用@JsonCreator进行注释。

您需要发送Content-Type标题(如第二个示例中所示),否则Spring并不知道您正在发送json。