【问题标题】:Spring POST Request Unsupported Media Type 415Spring POST 请求不受支持的媒体类型 415
【发布时间】:2015-06-17 10:41:17
【问题描述】:

我有一个简单的 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;
    }
}

您知道导致此问题的原因以及如何解决它吗?谢谢!

【问题讨论】:

    标签: java json spring spring-mvc


    【解决方案1】:

    没有找到适合类型[简单类型,类 org.elsys.internetprogramming.trafficspy.server.Marker]的构造函数

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

    【讨论】:

    • 就是这样。谢谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 2019-01-16
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2013-08-08
      相关资源
      最近更新 更多