ikevin8me
ikevin8me

Reputation: 4313

How to accept JSON POJOs using embedded Jetty?

How do I configure embedded Jetty and Java Jersey to accept JSON POJO? (The configuration should be done programatically, not in a web.xml file.)

I'm using Jetty 9.4.1 Java Jersey 2.25.1

(I've search through online and more information are for outdated versions of Jetty / Jersey or it is meant for .xml file configuration.)

Upvotes: 1

Views: 739

Answers (2)

ikevin8me
ikevin8me

Reputation: 4313

I resolved it myself. I didn't add any dependency. I just added a method String parameter:

@PUT
@Path("/{accountID}")
@Consumes("application/json")
@Produces("application/json")
public final Response testPut(String jsonString) { ...

and indeed it caught the JSON input String!

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 208944

If you just add the following dependency

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

This should be enough. The required registration of the JacksonFeature should 1 be automatically performed.

1 - should because if you are creating an uber jar, you need to be careful that you are not overwriting the required file needed for auto-discovery. Generally this can be avoided by using the maven-shade-plugin. This is further discussed in MessageBodyProviderNotFoundException while running jar from command line.

See also:

Upvotes: 1

Related Questions