Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How register JacksonFeature on ClientConfig

How to give back JSON on resfull java project using jackson?

Following the article http://www.vogella.com/tutorials/REST/article.html (8.4. Create a client ) about create rest project in java, it says that return json proceed like:

public class TodoTest {

  public static void main(String[] args) {
    ClientConfig config = new new ClientConfig().register(JacksonFeature.class);
    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getBaseURI());
    System.out.println(target.path("rest").path("todo").request()
        .accept(MediaType.APPLICATION_JSON).get(String.class));
  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080_com.vogella.jersey.jaxb").build();
  }
} 

@XmlRootElement
public class Todo {
    private String id;
    private String summary;
    private String description;
    //getter/setter
}

But JacksonFeature.class is not in Jackson.jar. I'm using

jackson-annotations-2.8.2.jar
jackson-core-2.8.2.jar
jackson-databind-2.8.2.jar
jackson-jaxrs-base-2.8.2.jar
jackson-jaxrs-json-provider-2.8.2.jar
jackson-module-jaxb-annotations-2.8.2.jar

How could i solve it?

like image 997
ulima69 Avatar asked Dec 28 '25 00:12

ulima69


1 Answers

JacksonFeature is from the Jersey set of libraries (the jersey-media-json-jackson artifact in Maven), not Jackson. See documentation here. It is a way to tell Jersey "please use the Jackson libraries for JSON parsing and serialization", and is not part of Jackson.

It seems this is only one of the ways to get Jersey to use Jackson. This article states that placing the following XML in your pom.xml is enough to get Jersey to use Jackson. It also suggests that this is a better way and that the Jersey documentation may be wrong to encourage use of JacksonFeature.class. The linked article also recommends that you DO NOT use jersey-media-json-jackson.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>${jackson.version}</version>
</dependency>
like image 184
tonicsoft Avatar answered Dec 30 '25 12:12

tonicsoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!