[英]MessageBodyWriter not found for media type=application/json
I'm making a small RESTful service using Jetty.我正在使用 Jetty 制作一个小的 RESTful 服务。 Using Maven as the build tool.
使用 Maven 作为构建工具。
When I try to call a GET method that tries to access a JSON representation of an object, I get an "MessageBodyWriter" error.当我尝试调用尝试访问对象的 JSON 表示的 GET 方法时,我收到“MessageBodyWriter”错误。 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, GtfsRtFeedModel 的 GtfsRtFeedModel 如下,
@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,我的 Maven 文件包括以下内容以及码头依赖项,
<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.大多数类似的问题似乎已经通过添加
jersey-media-moxy
和所需的依赖项得到解决。 But since I've already have them on my maven file, I can't figure out what I'm missing.但是由于我的 Maven 文件中已经有了它们,我无法弄清楚我错过了什么。
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
.通常,可自动发现的功能会注册
MoxyJsonFeature
。 I am not sure how it works wit the embedded Jetty.我不确定它是如何与嵌入式 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.有一个
<init-param>
允许我们添加任意数量的提供者,用逗号分隔。 In a web.xml it would be在 web.xml 中,它将是
<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. MoxyJsonFeature
注册了MessageBodyWriter
和MessageBodyReader
以便在我们的 POJOS 与 JSON 之间进行编组和解组。 See more at JAX-RS Entity Providers , and continued in Support for Common Media Type Representations在JAX-RS 实体提供程序中查看更多信息,并在对通用媒体类型表示的支持中继续
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.