如何在泽西发布泛型?

————–通用类型——————

public class Pair<KT, VT> {
    private KT key;
    private VT value;

    public Pair() {
    }

    public Pair(KT key, VT value) {
        this.key = key;
        this.value = value;
    }
}

————- Restful API ————

@Path("/test")
public class TestApi {
    @GET
    @Path("/query")
    @Produces(MediaType.APPLICATION_JSON)
    public Pair query(@DefaultValue("0") @QueryParam("key") int key) {
        return new Pair(key, "value:" + key);
    }

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public String create(Pair<Integer, String> pair){
        return "put success : " + pair.toString();
    }
}

– – – – – – -问题 – – – – – – –

GET:http://localhost:8080/api/test/query的响应是:

{ “键”:{ “类型”: “INT”, “值”:0}, “值”:{ “类型”: “串”, “值”: “值:0”}}

但是,当我使用“Content-Type:application / json”标题将相同的json发布到POST:http://localhost:8080/api/test/post时,会发生异常:
《如何在泽西发布泛型?》

谁能告诉我如何发布泛型类型的对象?

—————的pom.xml ——————

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.23.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jetty.version>9.0.6.v20130930</jetty.version>
    </properties>

最佳答案 这不应该是对GET请求的正确响应

{"key":{"type":"int","value":0},"value":{"type":"string","value":"value:0"}}

它应该是

{"key":0,"value":"value:0"}

这只是使用MOXy的许多危险之一,这是Jersey的默认JSON提供程序(在Glassfish中).如果您使用Jackson,则应该看到正确的行为.

要使用Jackson,您可以通过将属性ServerProperties.MOXY_JSON_FEATURE_DISABLE设置为true来禁用MOXy(防止Jackson注册).或者您可以添加The Jackson依赖项,然后在应用程序中注册JacksonFeature.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

进行更改后,它应该适用于GET和POST请求.

UPDATE

测试来源.运行测试时,您还应该在日志中看到请求的正确输出,它只返回与发布的相同的对.

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * Required dependencies for test
 * 
 *  <dependency>
 *      <groupId>org.glassfish.jersey.containers</groupId>
 *      <artifactId>jersey-container-grizzly2-http</artifactId>
 *      <version>2.23</version>
 *  </dependency>
 *  <dependency>
 *      <groupId>org.glassfish.jersey.media</groupId>
 *      <artifactId>jersey-media-json-jackson</artifactId>
 *      <version>2.23</version>
 *  </dependency>
 *
 * @author Paul Samsotha
 */
public class JacksonGenericTest extends JerseyTest {

    @XmlRootElement
    public static class Pair<K, V> {

        private K key;
        private V value;

        public Pair() {
        }

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pair{" + "key=" + key + ", value=" + value + '}';
        }
    }

    @Path("test")
    @Consumes("application/json")
    @Produces("application/json")
    public static class TestResource {

        @POST
        public Pair create(Pair<Integer, String> pair) {
            return pair;
        }
    }

    @Override
    public ResourceConfig configure() {
        return new ResourceConfig(TestResource.class)
                // not required here since we don't have MOXy on the classpath,
                // but in Glassfish, you should make sure to register it
                // so it disables MOXy, since Glassfish has MOXy on the classpath.
                .register(JacksonFeature.class)
                .register(new LoggingFilter(Logger.getAnonymousLogger(), true));
    }

    private final String jsonData = "{\"key\":100,\"value\":\"value:100\"}";

    @Test
    public void postTest() {
        final Response response = target("test")
                .request()
                .post(Entity.json(jsonData));
        final Pair<Integer, String> pair = response.readEntity(
                new GenericType<Pair<Integer, String>>(){});
        assertThat(pair.getKey(), is(100));
        assertThat(pair.getValue(), is("value:100"));
    }
}
点赞