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

标签 java json spring spring-mvc

我有一个简单的 Spring 4 项目,基于 this tutorial .我正在尝试实现一个 RESTful 接口(interface)。现在我在处理 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 header ,我有这个:

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

}

标记.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;
    }
}

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

最佳答案

No suitable constructor found for type [simple type, class org.elsys.internetprogramming.trafficspy.server.Marker]

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

关于java - Spring POST 请求不支持的媒体类型 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29582994/

相关文章:

Spring JPA CrudRepository save(Entity) 在 id 字段中返回 0

java - 未检测到闪光灯

Java bug 扩展

java - 在接口(interface)的所有实现上调用基函数

json - Zapier Catch (Raw) Hook JSON 解析问题

java - 在 Spring Boot 中使用两个数据源

java - environment.getProperty ("property") 是否产生与 @Value ("property"相同的值)

json - 将键值数组存储到紧凑的JSON字符串中

javascript - 如何复制 SharePoint 2010 内部进行的社交 ajax 调用?

java - 如何遵循 Effective Java 建议?