使用泽西和灰熊启用JSON

时间:2021-10-22 03:11:41

I am playing with Jersey hosted with Grizzly and want to be able to consume and produce JSON, however I am getting 500 from the server in get request and media type unsupported in POST my server code is

我正在和Grizzly托管的Jersey一起玩,并希望能够使用和生成JSON,但是我从服务器获取500请求和POST不支持的媒体类型我的服务器代码是

org.glassfish.jersey.server.ResourceConfig rc = new ResourceConfig();
    rc.packages("RestServer.controllers");

    final Map<String, Object> initParams = new HashMap<String, Object>();
    initParams.put("com.sun.jersey.config.property.packages", "rest");
    initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");

    rc.addProperties(initParams);

    webServer = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);

my POJO is:

我的POJO是:

@XmlRootElement
public class Dummy {

    private int id;
    private String name;

    public Dummy(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @XmlElement(name="id")
    public int getId() {
        return id;
    }

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }
}

and controller

@Path("/Dummies")
public class DummyController {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        System.out.println("Get");
        return Response.status(Status.OK)
            .entity(new Dummy(-1, "hello"))
            .build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response AddDummy(Dummy dummy) {
        return Response.status(Status.CREATED).entity(dummy).build();
    }
}

1 个解决方案

#1


14  

You're using Jersey 2. In Jersey 2, there is no com.sun.jersey.api.json.POJOMappingFeature and com.sun.jersey.config.property.packages, so you can get rid of those.

你正在使用Jersey 2.在Jersey 2中,没有com.sun.jersey.api.json.POJOMappingFeature和com.sun.jersey.config.property.packages,所以你可以摆脱它们。

To enable JSON to/from POJO, we need a MessageBodyWriter/MessageBodyReader(see more here). Fortunately, we don't need to write these ourselves. You just need a dependency (see here).

要从POJO启用JSON,我们需要MessageBodyWriter / MessageBodyReader(请参阅此处)。幸运的是,我们不需要自己写这些。你只需要一个依赖(见这里)。

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

This depedency will pull in the required Jackson MessageBodyReader/MessageBodyWriter. Then just configure it

这种依赖性将引入所需的Jackson MessageBodyReader / MessageBodyWriter。然后只需配置它

resourceConfig.register(JacksonFeature.class);

#1


14  

You're using Jersey 2. In Jersey 2, there is no com.sun.jersey.api.json.POJOMappingFeature and com.sun.jersey.config.property.packages, so you can get rid of those.

你正在使用Jersey 2.在Jersey 2中,没有com.sun.jersey.api.json.POJOMappingFeature和com.sun.jersey.config.property.packages,所以你可以摆脱它们。

To enable JSON to/from POJO, we need a MessageBodyWriter/MessageBodyReader(see more here). Fortunately, we don't need to write these ourselves. You just need a dependency (see here).

要从POJO启用JSON,我们需要MessageBodyWriter / MessageBodyReader(请参阅此处)。幸运的是,我们不需要自己写这些。你只需要一个依赖(见这里)。

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

This depedency will pull in the required Jackson MessageBodyReader/MessageBodyWriter. Then just configure it

这种依赖性将引入所需的Jackson MessageBodyReader / MessageBodyWriter。然后只需配置它

resourceConfig.register(JacksonFeature.class);