I am attempting to use the @JsonDeserialize(builder = class)
annotation example for Jackson to allow for the builder pattern. I tried to use my objects but kept running into an error. I then copied this example from the Jackson webpage, but it is still not working. Here is my sample files:
Data Item
package org.cpicard.finance;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(builder=ExampleBuilder.class)
public class Example {
private final int x, y;
protected Dummy(int x, int y) {
this.x = x;
this.y = y;
}
}
Builder
package org.cpicard.finance;
public class ExampleBuilder {
private int x, y;
public ExampleBuilder () { }
public ExampleBuilder withX(int x) {
this.x = x;
return this;
}
public ExampleBuilder withY(int y) {
this.y = y;
return this;
}
public Example build() {
return new Example(x, y);
}
}
Rest Service
package org.cpicard.finance;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.cpicard.model.finance.Example;
@Path("/account")
public class AccountService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("test1")
public Response createAccount1(final Example account) {
System.out.println(account.toString());
return Response.ok().build();
}
}
Application Config
package org.cpicard.finance;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rs")
public class ApplicationConfig extends Application {
private final Set<Class<?>> classes;
public ApplicationConfig() {
final HashSet<Class<?>> c = new HashSet<>();
c.add(AccountService.class);
classes = Collections.unmodifiableSet(c);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
}
Web xml Snippet
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.cpicard.finance.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
This results in the following error:
[2015-03-01T10:30:19.576-0700] [glassfish 4.1] [SEVERE] [] [org.glassfish.jersey.message.internal.ReaderInterceptorExecutor] [tid: _ThreadID=31 _ThreadName=http-listener-1(4)] [timeMillis: 1425231019576] [levelValue: 1000] [[ MessageBodyReader not found for media type=application/json, type=class org.cpicard.model.finance.Example, genericType=class org.cpicard.model.finance.Example.]]
When I switch the data item to a standard object that has regular getters and setters it works fine. I am thinking there is something incorrect in my configuration, but not sure what it could be.
Using Glassfish 4.1
Aucun commentaire:
Enregistrer un commentaire