在java REST中解析JSON参数时出错 [英] Error parsing JSON param in java REST

查看:134
本文介绍了在java REST中解析JSON参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级了我的REST API以使用jersey 2.x,现在我无法像以前那样检索JSON body params,这些方法根本就不再被调用了。我的猜测是我错过了将JSON解析为java对象的依赖关系,但是我不太清楚我需要添加什么,任何帮助都有所帮助。

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. 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.

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方法

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

FollowUserBean.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FollowUserBean {
    public Integer friendId;

    public FollowUserBean() {}
}


推荐答案

您需要一个JSON提供程序



在编写本文时,Jersey 2.x与以下模块集成以提供JSON支持:

You need a JSON provider

At time of writing, Jersey 2.x integrates with the following modules to provide JSON support:

  • MOXy
  • Java API for JSON Processing (JSON-P)
  • Jackson
  • Jettison

请参阅下面使用Jackson作为Jersey 2.x的JSON提供程序所需的步骤:

See below the steps required to use Jackson as a JSON provider for Jersey 2.x:

添加Jack子模块依赖

要使用Jackson 2.x作为您的JSON提供者,您需要添加 jersey-media-json-jackson 模块到 pom.xml 文件:

To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson module to your pom.xml file:

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

使用Jackson 1.x它看起来像:

To use Jackson 1.x it'll look like:

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

注册Jackson模块

除了添加上述依赖项外,您还需要注册 JacksonFeature (或 Jackson1xa​​ture 杰克逊1.x)在你的 申请 / ResourceConfig 子类:

Besides adding the dependency mentioned above, you need to register JacksonFeature (or Jackson1Feature for Jackson 1.x) in your Application / ResourceConfig subclass:

@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);
    }
}

如果你没有 申请 / ResourceConfig 子类,你可以注册 web.xml中的jersey / jackson / JacksonFeature.htmlrel =nofollow noreferrer> JacksonFeature 部署描述符。可以在逗号分隔值中提供特定资源,提供程序和功能完全限定的类名称jersey / org / glassfish / jersey / server / ServerProperties.html#PROVIDER_CLASSNAMESrel =nofollow noreferrer> jersey.config.server.provider.classnames 初始化参数。

If you don't have an Application / ResourceConfig subclass, you can register the JacksonFeature in your web.xml deployment descriptor. 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.

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

MessageBodyWriter JacksonJsonProvider

有关详细信息,请查看Jersey 文档,关于对常见媒体类型表示的支持。

For more details, check the Jersey documentation about support for common media type representations.

这篇关于在java REST中解析JSON参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆