首页 > 解决方案 > 用于消费 JSON 文件的 JAX-RS 生成错误 415

问题描述

我想创建一个带有 JAX-RS 和 Jersey 的 REST 服务,用于使用 JSON 结构。

我正在使用 org.glassfish.jersey 和 gradle,所以我插入了 gradle-build 文件:

compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet-core', version: '2.26-b03'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-common', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.26-b03'

我在 web.xml 中激活了 POJO 映射功能:

<init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
</init-param>

我编写了一个用于管理 JSON 实体的简单类,并使用了 XMLElement 注释。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="polygongjson")
public class PolygonGJson {

    @XmlElement(name="type")
    String type;
    @XmlElement(name="properties")
    String properties;
    @XmlElement(name="geometry")
    String geometry;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getProperties() {
        return properties;
    }

    public void setProperties(String properties) {
        this.properties = properties;
    }

    public String getGeometry() {
        return geometry;
    }

    public void setGeometry(String geometry) {
        this.geometry = geometry;
    }

    @Override
    public String toString() {
        return "Track [type=" + type + ", properties=" + properties + ", geometry=" + geometry +"]";
    }
}

最后,我创建了通过 POST 使用 JSON 结构的 REST 方法。

Path("/upload")
public class UploadRaster extends Application {

    @POST
        @Consumes(MediaType.APPLICATION_JSON)
        @Path("/j_geojson/{id_user}")
        public Response uploadGeoJSON(@PathParam("id_user") String id_user,
                                      PolygonGJson polyIn){


            try{

                System.out.println("JSON: "+polyIn.toString());
                return Response.status(200).entity(polyIn.getGeometry()).build();

            }catch(Exception e){

                return Response.status(500).entity(e.getMessage()).build();
            }
        }
}

我准备了一个简单的 json 文件:

{
   "type": "Feature",
   "properties": "test prop",
   "geometry": "test geo"
}

为了测试我的方法,我以这种方式使用 curl:

curl -X POST -H "Content-Type: application/json" -d @./testgjsimple.json https://myserver:8443/mywarfile/api/upload/j_geojson/1

但服务器响应:

HTTP Status 415 - Unsupported Media Type
Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

注意:我的应用服务器是 Tomcat 7。

任何想法?

标签: jsonrestgradlejerseyjax-rs

解决方案


据我所知,XML 和 JSON 结构的处理方式不同。所以这部分:

...
@XmlRootElement(name="polygongjson")
public class PolygonGJson {
    @XmlElement(name="type")
    String type;
...

可能导致无法正确使用 JSON。

在没有这样的注释的情况下尝试它(它必须工作):

...
public class PolygonGJson {
    String type;
...

或者如果您想调整一些特殊的东西,请使用 JSON 的注释(我更喜欢杰克逊)。


推荐阅读