I'm making a small RESTful service using Jetty. Using Maven as the build tool.
When I try to call a GET method that tries to access a JSON representation of an object, I get an "MessageBodyWriter" error. The method in question is as follows,
@Path("/gtfs-rt-feed")
public class GtfsRtFeed {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getGtfsRtFeed(){
GtfsRtFeedModel feedInfo = new GtfsRtFeedModel();
feedInfo.setStartTime(121334);
feedInfo.setGtfsId(1);
feedInfo.setGtfsUrl("http://www.google.com");
Gson gson = new Gson();
return Response.ok(feedInfo).build();
}
...
The GtfsRtFeedModel for the GtfsRtFeedModel is as follows,
@XmlRootElement
public class GtfsRtFeedModel {
private String gtfsUrl;
private int gtfsId;
private long startTime;
public GtfsRtFeedModel(){}
public String getGtfsUrl() {
return gtfsUrl;
}
...//Getters and setters for all private variables below
My Maven file includes the following along with the jetty dependencies,
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.19</version>
</dependency>
Most of the similar questions seems to have been resolved by adding jersey-media-moxy
and the required dependencies. But since I've already have them on my maven file, I can't figure out what I'm missing.
Any help would be appreciated.
EDIT: Jetty code added My Jetty code is as follows,
public static void main(String[] args) throws Exception{
GTFSDB.InitializeDB();
Datasource ds = Datasource.getInstance();
ds.getConnection();
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.setResourceBase(BASE_RESOURCE);
server.setHandler(context);
context.addServlet(RTFeedValidatorServlet.class, "/validate");
context.addServlet(GTFSDownloaderServlet.class, "/downloadgtfs");
context.addServlet(FeedInfoServlet.class, "/feedInfo");
context.addServlet(TriggerBackgroundServlet.class, "/startBackground");
context.addServlet(GetFeedJSON.class, "/getFeed");
context.addServlet(DefaultServlet.class, "/");
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/api/*");
jerseyServlet.setInitOrder(1);
jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "edu.usf.cutr.gtfsrtvalidator.api.resource");
server.start();
server.join();
}
Normally the auto-discoverable feature registers the MoxyJsonFeature
. I am not sure how it works wit the embedded Jetty. But if it is not register, we can register it ourselves. There is an <init-param>
that allows us to add a arbitrary number of providers, separated by a comma. In a web.xml it would be
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.moxy.json.MoxyJsonFeature,
org.bar.otherresources.MyCatResource
</param-value>
</init-param>
So in your Jetty code, you could do
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
"org.glassfish.jersey.moxy.json.MoxyJsonFeature");
The MoxyJsonFeature
registers the MessageBodyWriter
and MessageBodyReader
required to marshal and unmarshal our POJOS to and from JSON. See more at JAX-RS Entity Providers , and continued in Support for Common Media Type Representations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.