Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MessageBodyProviderNotFoundException在使用JSON对象数组执行Post时发生_Java_Json_Rest_Post_Jersey 2.0 - Fatal编程技术网

Java MessageBodyProviderNotFoundException在使用JSON对象数组执行Post时发生

Java MessageBodyProviderNotFoundException在使用JSON对象数组执行Post时发生,java,json,rest,post,jersey-2.0,Java,Json,Rest,Post,Jersey 2.0,我正在创建我的第一个web服务,因此可能缺少一些非常简单的东西。 我在EclipseKepler中使用Jersey 2.x创建了一个web服务,在Tomcat上没有Maven,它正在处理没有参数的“@GET”请求(通过浏览器和客户端应用程序进行测试),但我在“@POST”上遇到了问题(代码如下)。这实际上是一个带有非常复杂过滤条件的get请求 @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_J

我正在创建我的第一个web服务,因此可能缺少一些非常简单的东西。 我在EclipseKepler中使用Jersey 2.x创建了一个web服务,在Tomcat上没有Maven,它正在处理没有参数的“@GET”请求(通过浏览器和客户端应用程序进行测试),但我在“@POST”上遇到了问题(代码如下)。这实际上是一个带有非常复杂过滤条件的get请求

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String getFilteredPictures(ArrayList<FilterOption> filters)
{
    PictureProvider provider = new PictureProvider();
    ArrayList<PictureInfo> pictures;
    try
    {
        pictures = provider.getPictures(filters);
        Gson gson = new Gson();
        return gson.toJson(pictures);
    }
    catch (SQLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}
@POST
@使用(MediaType.APPLICATION_JSON)
@产生(MediaType.APPLICATION_JSON)
公共字符串getFilteredPictures(ArrayList筛选器)
{
PictureProvider=新建PictureProvider();
阵列列表图片;
尝试
{
图片=提供者.getPictures(过滤器);
Gson Gson=新的Gson();
返回gson.toJson(图片);
}
捕获(SQLE异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
}
我创建了一个虚拟客户端,只是为了看看上面的方法是否有效:

HttpClient httpclient = new DefaultHttpClient();
    Gson gson = new Gson();
    HttpPost request = new HttpPost(SERVICE_URI + picturesServiceEndPoint);
     //create dummy data
    ArrayList<FilterOption> filters = new ArrayList<>();
    ArrayList<String> options = new ArrayList<>();
    options.add("Black");
    filters.add(new FilterOption("Color", options));
    StringEntity postParam = StringEntity(gson.toJson(filters), "UTF-8");
    postParam.setContentType("application/json");

    request.setEntity(postParam);
    request.setHeader("Accept", "application/json");
    try
    {
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null)
        {
            //obtain results..

        }
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
HttpClient-HttpClient=newdefaulthttpclient();
Gson Gson=新的Gson();
HttpPost请求=新的HttpPost(服务URI+PictureServiceEndpoint);
//创建虚拟数据
ArrayList过滤器=新的ArrayList();
ArrayList选项=新建ArrayList();
选项。添加(“黑色”);
添加(新过滤器选项(“颜色”,选项));
StringEntity postParam=StringEntity(gson.toJson(过滤器),“UTF-8”);
setContentType(“应用程序/json”);
请求。setEntity(后参数);
setHeader(“接受”、“应用程序/json”);
尝试
{
HttpResponse response=httpclient.execute(请求);
HttpEntity=response.getEntity();
如果(实体!=null)
{
//取得成果。。
}
}
捕获(客户端协议例外e)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
当我运行客户端时,服务器抛出以下异常“.MessageBodyProviderNotFoundException:MessageBodyReader找不到媒体类型=应用程序/json”:

我怀疑问题在于它无法将JSON转换为POJO对象,所以我在web.xml中添加了init参数,但没有效果。此外,我尝试只发送一个FilterOption对象,认为ArrayList太复杂了,但同样没有效果


感谢您的时间:)

如果您告诉JAX-RS如何做,它只能在
过滤器选项
实例之间转换JSON

一种常见的方法是使用JAXB实现这一点:

@XmlRootElement
class FilterOption {
    // members, getters, setters, constructors
}

如果您告诉JAX-RS如何转换JSON,那么它只能在
FilterOption
实例之间转换JSON

一种常见的方法是使用JAXB实现这一点:

@XmlRootElement
class FilterOption {
    // members, getters, setters, constructors
}

我找到了一种避免预期解决方案的方法。我只是在处理字符串,并使用gson库解析它们:

@POST
// @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_JSON)
public String getFilteredPictures(String jsonFilters)
{
    PictureProvider provider = new PictureProvider();
    ArrayList<PictureInfo> pictures = null;
    ArrayList<FilterOption> filters = null;
    if (jsonFilters != null)
    {
        Type collectionType = new TypeToken<ArrayList<FilterOption>>()
        {}.getType();
        filters = gson.fromJson(jsonFilters, collectionType);
    }
    .....
@POST
//@Consumes(MediaType.APPLICATION_JSON)
//@products(MediaType.APPLICATION_JSON)
公共字符串getFilteredPictures(字符串jsonFilters)
{
PictureProvider=新建PictureProvider();
ArrayList图片=null;
ArrayList过滤器=null;
if(jsonFilters!=null)
{
Type collectionType=new-TypeToken()
{}.getType();
filters=gson.fromJson(jsonFilters,collectionType);
}
.....

我找到了一种避免预期解决方案的方法。我只是在处理字符串并使用gson库解析它们:

@POST
// @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_JSON)
public String getFilteredPictures(String jsonFilters)
{
    PictureProvider provider = new PictureProvider();
    ArrayList<PictureInfo> pictures = null;
    ArrayList<FilterOption> filters = null;
    if (jsonFilters != null)
    {
        Type collectionType = new TypeToken<ArrayList<FilterOption>>()
        {}.getType();
        filters = gson.fromJson(jsonFilters, collectionType);
    }
    .....
@POST
//@Consumes(MediaType.APPLICATION_JSON)
//@products(MediaType.APPLICATION_JSON)
公共字符串getFilteredPictures(字符串jsonFilters)
{
PictureProvider=新建PictureProvider();
ArrayList图片=null;
ArrayList过滤器=null;
if(jsonFilters!=空)
{
Type collectionType=new-TypeToken()
{}.getType();
filters=gson.fromJson(jsonFilters,collectionType);
}
.....

通过实现MessageBodyWriter和MessageBodyReader两个接口,您仍然可以保留方法的签名、媒体类型,并且仍然可以使用GSON封送/解封送

我手头没有自己项目的代码示例,但以下内容看起来不错:

通过实现MessageBodyWriter和MessageBodyReader两个接口,您仍然可以保留方法的签名、媒体类型,并且仍然可以使用GSON封送/解封送

我手头没有自己项目的代码示例,但以下内容看起来不错:

Jersey JSON支持作为一组扩展模块提供,其中每个模块都包含一个
功能的实现,该功能需要注册到
可配置的
实例(客户端/服务器)中。有多个框架支持JSON处理和/或JSON到Java绑定。下面列出的模块通过将各个JSON框架集成到Jersey中来支持JSON表示。目前,Jersey与以下模块集成以提供JSON支持:

  • MOXy-自Jersey 2.0以来,通过MOXy支持JSON绑定是在Jersey应用程序中支持JSON绑定的默认和首选方式。当JSON MOXy模块位于类路径上时,Jersey将自动发现该模块并在应用程序中通过MOXy无缝启用JSON绑定支持。(参见第4.3节,“自动发现功能”。)
  • 用于JSON处理的Java API(JSON-P)
  • 杰克逊
  • 抛弃
有关更多信息,请阅读泽西岛文档

莫西: Moxy是提议的json媒体支持方法。 MOXy媒体模块是一种不需要在客户机/服务器中显式注册其功能(MoxyJsonFeature)的模块,可配置,因为在将jersey媒体MOXy模块添加到类路径时,会自动发现并注册此功能

要使用MOXy作为JSON提供程序,您需要添加jersey media MOXy模块
 public static void main(String[] args) {
    //ClientConfig cc = new ClientConfig().register(new JacksonFeature());
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8084/myhost/test");

    Form form = new Form();
    form.param("x", "foo");
    form.param("y", "bar");

    MyJAXBBean bean;
    bean = target.request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),
                    MyJAXBBean.class);
    System.out.println(bean);
}
 {"name":"haleh", "id":3}