我正在创建我的第一个Web服务,所以可能是因为我缺少一些非常简单的东西。 我在Eclipse Kepler中使用Jersey 2.x在Tomcat上创建了一个没有Maven的Web服务,它正在为不带参数(从浏览器和客户端应用程序测试)的“@GET”请求工作,但我遇到了“@POST” (代码如下)。这实际上是一个具有非常复杂的过滤条件的获取请求。MessageBodyProviderNotFoundException执行带有JSON数组对象的Post对象
@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;
}
}
我创建了一个虚拟的客户端,只看到上面的方法工作:
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();
}
当我运行客户端对服务器抛出以下异常” .MessageBodyProviderNotFoundException:化MessageBodyReader找不到媒体类型=应用程序/ JSON“:
我怀疑的问题是,它不能转换JSON到我的POJO对象,所以我把一个初始参数在web.xml中我的,但它没有任何效果。另外,我试着发送一个FilterOption对象,认为ArrayList太复杂了,但它又没有效果。
感谢您的时间:)
请包含'FilterOption'的代码。它有JAXB注释吗? – 2013-10-03 15:45:20
不,它没有注释。 – Laviniux
阅读泽西文档第9章:[支持常见媒体类型表示] [1] –