Developing JAX-RS app and struggle a problem. Need to customize json-output from my resource so configured Jersey(2.22.2) to use Jackson(2.5) parser instead default Moxy (according to this answer).
Here is pom.xml fragment
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I also configured web.xml file to use Jackson by default
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.mycompany.myresourcepackage
com.fasterxml.jackson.jaxrs
</param-value>
</init-param>
But resource output is deffer from what I configure with annotations and serializer. Here is model to represent as json
@XmlRootElement(name = "geo")
public class Geometry {
public Geometry() {
coordinates = new ArrayList<List<Double>>();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@JsonSerialize(using = CoordinatesSerializer.class)
public List<List<Double>> getCoordinates() {
return coordinates;
}
public void setCoordinates( List<List<Double>> coordinates) {
this.coordinates = coordinates;
}
@JsonProperty("tp")
private String type;
private List<List<Double>> coordinates;
}
And serializer
protected CoordinatesSerializer(Class<List<List<Double>>> t) { }
private static final long serialVersionUID = 1L;
@Override
public void serialize(List<List<Double>> value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
try {
jgen.writeArrayFieldStart("motherfucking-coordinates");
int coordinates_size = value.size();
for (int i = 0; i < coordinates_size; i++) {
jgen.writeStartArray();
jgen.writeNumber(value.get(i).get(0));
jgen.writeNumber(value.get(i).get(1));
jgen.writeEndArray();
}
jgen.writeEndArray();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
And here is resource fragment
@GET
@Path("/route/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Geometry(@PathParam("id") int id) {
// construct and return object
}
Response json is not customized at all.
{"geometry":{"coordinates":["27.56 53.9","27.58 53.88","27.55 53.94"],"type":"LineString"},"id":"1","type":"Feature"}
Desired output is
{"geometry":{"coordinates":[[27.56, 53.9],[27.58, 53.88],[27.55, 53.94]],"type":"LineString"},"id":"1","type":"Feature"}
Thanks a lot.
>` writes data in the format you are looking for.
– Justin Jose Mar 30 '17 at 08:35