【问题标题】:JAX-RS for consuming JSON file generates Error 415用于消费 JSON 文件的 JAX-RS 生成错误 415
【发布时间】:2020-05-06 09:42:43
【问题描述】:

我想创建一个带有 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。

有什么想法吗?

【问题讨论】:

    标签: json rest gradle jersey jax-rs


    【解决方案1】:

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

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

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

    在没有类似注释的情况下尝试它(它必须有效):

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

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

    【讨论】:

    • 感谢您的建议,我在 Jersey 文档和管理 JSON 结构以及 Xml 结构的示例之后使用了 Xml 注释,我正在寻找特定的 JSON 注释,但我找不到任何东西。 ..无论如何我会尝试你的建议
    • 我从不使用杰克逊,我不知道杰克逊和球衣的区别
    • 很遗憾,您的解决方案不起作用。我会尝试使用 Jackson 库
    【解决方案2】:

    我在 Jersey 2.33 中也遇到了这个问题。仔细阅读文档后:https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/media.html#json.json-b

    我发现我们需要注册 json 提供程序,除非我们使用默认提供程序:jersey-media-json-binding。

    因此,对于您的情况,您可以添加另一个依赖项:

    compile "org.glassfish.jersey.media:jersey-media-json-binding:2.26"

    【讨论】:

    • 感谢您的建议,我会尽快尝试
    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多