简体   繁体   English

在Java REST中解析JSON参数时出错

[英]Error parsing JSON param in java REST

I have recently upgraded my REST API to use jersey 2.x and now I am unable to retrieve JSON body params the way I used to, the methods simply do not get called anymore. 我最近将REST API升级为使用jersey 2.x,现在我无法以以前的方式检索JSON主体参数,因此不再调用这些方法。 My guess is I'm missing a dependency to parse the JSON to a java object but I'm not too sure what i need to add in, any help appreciated. 我的猜测是我缺少将JSON解析为java对象的依赖项,但是我不太确定需要添加什么,感谢任何帮助。

pom.xml pom.xml

<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.19</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.19</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.19</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22</version>
    </dependency>
</dependencies>

REST method REST方式

@POST
    @Path("/users/{userId}/friends")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response followUser(@PathParam("userId") Integer myUserId, FollowUserBean user) {}

FollowUserBean.java FollowUserBean.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FollowUserBean {
    public Integer friendId;

    public FollowUserBean() {}
}

You need a JSON provider 您需要一个JSON提供程序

At time of writing, Jersey 2.x integrates with the following modules to provide JSON support: 在撰写本文时,Jersey 2.x与以下模块集成以提供JSON支持:

Using Jackson 使用杰克逊

See below the steps required to use Jackson as a JSON provider for Jersey 2.x: 请参阅以下将Jackson用作Jersey 2.x的JSON提供程序所需的步骤:

Adding Jackson module dependencies 添加Jackson模块依赖项

To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson module to your pom.xml file: 要将Jackson 2.x用作JSON提供程序,您需要将jersey-media-json-jackson模块添加到pom.xml文件中:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.25.1</version>
</dependency>

To use Jackson 1.x it'll look like: 要使用Jackson 1.x,它将类似于:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson1</artifactId>
    <version>2.25.1</version>
</dependency>

Registering Jackson module 注册Jackson模块

Besides adding the dependency mentioned above, you need to register JacksonFeature (or Jackson1Feature for Jackson 1.x) in your Application / ResourceConfig subclass: 除了增加依赖上面提到的,你需要注册JacksonFeature (或Jackson1Feature在杰克逊的1.x) Application / ResourceConfig子类:

@ApplicationPath("/api")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(JacksonFeature.class);
        return classes;
    }
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(JacksonFeature.class);
    }
}

If you don't have an Application / ResourceConfig subclass, you can register the JacksonFeature in your web.xml deployment descriptor. 如果没有Application / ResourceConfig子类,则可以在web.xml部署描述符中注册JacksonFeature The specific resource, provider and feature fully-qualified class names can be provided in a comma-separated value of jersey.config.server.provider.classnames initialization parameter. 可以在jersey.config.server.provider.classnames初始化参数的逗号分隔值中提供特定的资源,提供者和功能完全合格的类名称。

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>

The MessageBodyWriter provided by Jackson is JacksonJsonProvider . Jackson提供的MessageBodyWriterJacksonJsonProvider


For more details, check the Jersey documentation about support for common media type representations. 有关更多详细信息,请参阅Jersey 文档中有关对常见媒体类型表示形式的支持。

You may be missing Jersey JSON Jackson (2.x) entity providers support module: 您可能会缺少Jersey JSON Jackson(2.x)实体提供程序支持模块:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.19</version>
    <scope>compile</scope>
</dependency>

It is recommended to use the same Jersey version in all libs. 建议在所有库中使用相同的Jersey版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM