当前位置: 首页 > 知识库问答 >
问题:

如何在Rest服务中使用Jersey发送JSON对象

叶声
2023-03-14
    <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>
<?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类只是一个带有字符串的类。当我点击URL:http://localhost:8080/simple-service-webapp/webapi/myresource/This work时,我得到的是“get it!”

但是当我点击:http://localhost:8080/simple-service-webapp/webapi/myresource/complexobject/capo时

共有1个答案

史鸿运
2023-03-14

有了这种依赖关系

<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>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
</dependency>
 类似资料:
  • 问题内容: 我一直在搜寻我的屁股,试图找出如何做到这一点:我有一个Jersey REST服务。调用REST服务的请求包含一个JSON对象。我的问题是,从Jersey POST方法实现中,如何获得对HTTP请求正文中的JSON的访问? 任何提示,技巧,示例代码的指针,将不胜感激。 谢谢… -史蒂夫 问题答案: 我不确定如何获取JSON字符串本身,但是您当然可以获取其中包含的数据,如下所示: 定义一个

  • 问题内容: 我想使用POST方法将JSONObject发送到服务器。我已经使用凌空库通过字符串参数来正常工作,但是如果我尝试使用json对象,则在调用json对象时显示错误,这是我的代码 我的错误表单服务器是: 如何解决这个问题呢。我要添加 标题,请检查我的代码是否正确。请给我一个解决这个问题的建议 问题答案: JsonObjectRequest中的第三个参数用于以jsonobject形式传递发布

  • 问题内容: 我有一个REST Web服务配置如下: 我的Tomcat服务器的配置如下: 我创建的POJO如下所示: } 我创建一个测试客户端以测试createUser方法: 但是我收到以下错误: 奇怪的是,当我将所有内容更改为appliction / xml时,它都能正常工作 知道为什么Json不能为我工作吗? 问题答案: 您正在执行: 这是不对的。您应该将真实的JSON字符串发送到您的服务。 获

  • 您好,我正在开发一个iOS项目,我正在向我的REST web服务发送一个json数组,该服务使用JAVA和Jersey以及Google Gson。我在SO上看到了几个类似的问题,比如link,但我不知道如何找到解决方案。这里我向服务器发送JSON数组,它的结构如下 下面是我使用JSON数组的Java类 我需要创建Friends类并将json对象映射到该类吗?请帮我写一些代码 欢迎提供任何例子或解释

  • 问题内容: 我在下面给出了一个POJO,我希望将其作为JSON或XML格式放入服务器。 这就是我所做的 客户: 我在网上发现的示例都在使用WebResource。 我不知道如何使用WebTarget。我所做的事情取自于SO上的一些示例,但Entity.entity()给出了错误的未定义方法entity(friend,String)。 POJO 请指导如何执行此操作。 谢谢 问题答案: 泽西岛有两种

  • 下面给出了一个POJO,我想把它作为JSON或XML放到服务器上。 这就是我所做的 客户: 我在网上找到的例子是使用网络资源。 我不知道如何使用WebTarget。我所做的是从SO上找到的一些示例中获取的,但是Entity.entity()给出了错误未定义的方法实体(朋友,字符串)。 POJO 请指导如何做到这一点。 谢谢