Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring POST请求不支持的媒体类型415_Java_Json_Spring_Spring Mvc - Fatal编程技术网

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

Java Spring POST请求不支持的媒体类型415,java,json,spring,spring-mvc,Java,Json,Spring,Spring Mvc,我有一个简单的Spring4项目,基于。我正在尝试实现一个RESTful接口。现在我在处理POST请求时遇到了麻烦。当我尝试发布JSON对象时,会出现以下错误: { "timestamp":1428785473020, "status":415, "error":"Unsupported Media Type", "exception":"org.springframework.web.HttpMediaTypeNotSup

我有一个简单的Spring4项目,基于。我正在尝试实现一个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;
    }

}
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;
    }
}
Marker.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;
    }

}
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;
    }
}
你知道是什么原因导致这个问题,以及如何解决它吗?谢谢大家!

找不到适合类型[simple type,class org.elsys.internetprogramming.trafficspy.server.Marker]的构造函数


您的标记类没有默认构造函数。因此Jackson没有办法实例化它。

需要用注释来注释
标记器


您需要发送内容类型头(如第二个示例所示),否则spring不知道您正在发送json。

就是这样。非常感谢。