1. Jackson
2. JSON-B
@Path("example")
public class ExampleResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ExampleObject getExampleObject() {
ExampleObject example = new ExampleObject();
example.setId(1);
example.setName("Example");
return example;
}
}
public class ExampleObject {
private int id;
private String name;
// Getters and setters
public ExampleObject() {
}
public ExampleObject(int id, String name) {
this.id = id;
this.name = name;
}
}
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.34</version>
</dependency>
@Provider
public class JacksonJsonProviderConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonJsonProviderConfig() {
objectMapper = new ObjectMapper();
// Configure Jackson features
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
// ...
}
@Override
public ObjectMapper getContext(Class<?> type) {
return objectMapper;
}
}