【问题标题】:How can I send a JSON object with Jersey in a Rest Service如何在 Rest Service 中发送带有 Jersey 的 JSON 对象
【发布时间】:2015-10-23 18:46:20
【问题描述】:

我正在尝试使用 jersey 提供宁静的服务,因为我正在将 jersey 示例用于 maven 项目。所以这就是我得到的:

我的 pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>simple-service-webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>simple-service-webapp</name>

    <build>
        <finalName>simple-service-webapp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> 
            <artifactId>jersey-media-moxy</artifactId> </dependency> -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.6.0</version>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.19</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.example</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

资源

package com.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("/myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }

    @Path( "complexObject/{name}" )
    @GET
    @Produces( { MediaType.APPLICATION_JSON } )
    public ComplexObject complexObject( @PathParam( "name" ) String name ) {
        return new ComplexObject(name);
    }
}

ComplexObject 类只是一个带有字符串的类。 当我点击网址时:http://localhost:8080/simple-service-webapp/webapi/myresource/ 这项工作,我得到“知道了!”

但是当我点击时:@9​​87654322@

我在控制台上收到此错误:

严重:找不到媒体类型 = 应用程序/json 的 MessageBodyWriter, type=class com.example.ComplexObject, genericType=class com.example.ComplexObject。

我该如何解决这个问题?

【问题讨论】:

  • 我已经取消注释这个依赖jersey-media-moxy
  • 尝试直接使用jersey-media-json-jackson 而不是jersey-jaxrs-json-jackson。并在您的web.xml 中提供org.codehaus.jackson.jaxrs 作为jersey.config.server.provider.packages 的值,例如&lt;param-value&gt;com.example;org.codehaus.jackson.jaxrs&lt;/param-value&gt;
  • 干得好!我喜欢你。现在我需要将此升级为我的真正问题

标签: java maven jersey jax-rs


【解决方案1】:

有了这个依赖

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.6.0</version>
</dependency>

您仍然需要注册提供商。可以单独注册

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
    </param-value>
</init-param>

或者由于Jackson ExceptionMappers 也带有依赖项,您可能只想扫描整个包(只需将其添加到包列表中)

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>
        com.example,
        com.fasterxml.jackson.jaxrs.json
    </param-value>
</init-param>

除了使用上述依赖项之外,另一个选项是使用 Jersey 的“包装器”依赖项(它处理提供者的注册,以及其他一些事情,例如 Jackson Entity Filtering。刚刚使用

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

有了这个依赖,就需要注册。通过 Jersey 的auto-discoverable feature自动注册

【讨论】:

  • 谢谢,DRB 给了我解决方案。但你做了一个更好的答案……一个答案。太好了!
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 2019-07-16
  • 1970-01-01
相关资源
最近更新 更多