如何设计在JAX-RS Web服务中使用JSON的POST方法?

时间:2013-11-11 02:49:07

标签: java json web-services rest curl

我正在设计一个简单的 REST API ,并且 GET 方法正常工作,但我无法获得 POST 方法正常工作。

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response nuevaCancion(Cancion cancion) {

        Response response = Response.ok().build();
        Response negativeResponse = Response.notAcceptable(null).build();
        return new ConectorCliente("usr", "pass", "REST").insertarCancion(cancion) ? response : negativeResponse;
    }

所以我的方法收到一个Cancion类(类已经得到@XmlRootElement注释)实例,该实例应该是JSON格式,如果JSON中的数据一切正常,它将返回 200 代码和 406 Not Acceptable 代码。

我正试图通过这个curl语句来测试方法:

curl -X POST -d @cancion.json http://localhost:8080/REST/webresources/canciones --header "Content-Type:application/json"

其中cancion.json是一个包含Cancion实例的正确JSON表示的文件。

但是我得到了HTTP Status 415 - Unsupported Media Type代码。还要停留:服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。

我使用Tomcat作为服务器,使用Jersey和Jackson库作为REST。我的代码出了什么问题?

0 个答案:

没有答案