获取“找不到媒体类型= application / json”的MessageBodyWriter尝试通过JAX-RS Web服务发送JSON对象 [英] Obtaining "MessageBodyWriter not found for media type=application/json" trying to send JSON object through JAX-RS web service

查看:498
本文介绍了获取“找不到媒体类型= application / json”的MessageBodyWriter尝试通过JAX-RS Web服务发送JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过JAX-RS Web服务发送JSON对象。
我的文件web.xml是:

I am trying to send a JSON object through a JAX-RS web service. My file web.xml is:

<servlet>
 <description>JAX-RS Tools Generated - Do not modify</description>
 <servlet-name>JAX-RS Servlet</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>it.notifire</param-value>
  </init-param>
  <init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>

为我要发送的对象建模的类是:

The class that models the object I want to send is:

public class GPSCoordinate {

private float latitudine;
private float longitudine;

 public float getLatitudine() {
    return latitudine;
}
public void setLatitudine(float latitudine) {
     this.latitudine = latitudine;
}
public float getLongitudine() {
    return longitudine;
}
public void setLongitudine(float longitudine) {
    this.longitudine = longitudine;
}
}

根类资源是:

@Path("position")
public class Position {

    @Context
private UriInfo context;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public GPSCoordinate getHTML() {

        GPSCoordinate coord = new GPSCoordinate();
        coord.setLatitudine(90.45f);
        coord.setLongitudine(34.56f);

        return coord;
    }
}

现在,当我尝试访问服务时,我点我的浏览器到以下链接

Now, when i try to access the service I point my browser to the following link

http://localhost:8080/Notifire/jaxrs/position

我收到以下错误:

message org.glassfish.jersey .message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application / json的MessageBodyWriter

message org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json

在我的WEB-INF / lib文件夹中,我有最后一个版本的jersey JAX-RS实现(jaxrs-ri-2.5.jar)和jersey-json.jar存档。

In my WEB-INF/lib folder I have the last release of jersey JAX-RS implementation (jaxrs-ri-2.5.jar) and the jersey-json.jar archive.

任何帮助都将不胜感激。

Any help would be much appreciated.

推荐答案

尝试将 Genson 添加到您的类路径,它将自动启用JSON支持。

Try adding Genson to your classpath, it will automatically enable JSON support.

Genson是json和java / scala的数据绑定和流媒体库。它实现了JAX-RS的扩展点MessageBodyReader / Writer,允许jersey自动检测Genson并将其用于Json解析/写入。

Genson is a data-binding and streaming library for json and java/scala. It implements the extension points MessageBodyReader/Writer of JAX-RS, allowing jersey to automatically detect Genson and use it for Json parsing/writing.

你可以找到更多关于它的信息 Gensons与JaxRS的整合(jersey& cie )。

You can find some more infos about Gensons integration with JaxRS (jersey & cie).

这篇关于获取“找不到媒体类型= application / json”的MessageBodyWriter尝试通过JAX-RS Web服务发送JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆