Java Jax-RS MessageBodyReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24153827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jax-RS MessageBodyReader
提问by ArrowKneeous
I'm learning how the MessageBodyReader method works from the providers. I see the method returns an object and I'm not sure how to access the object from a service. Could I get an explanation on how to get the object returned from the reader class? This would help me apply a reading rule for all dto's. Thanks in advance!
我正在从提供者那里学习 MessageBodyReader 方法的工作原理。我看到该方法返回一个对象,但我不确定如何从服务访问该对象。我能否得到有关如何获取从读取器类返回的对象的解释?这将帮助我为所有 dto 应用阅读规则。提前致谢!
Service:
服务:
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/CreateAccount")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createAccount(@Context HttpServletRequest req) {
String a = "Reader success? ";//Would to see that string here!
return Response.ok().build();
}
Provider:
供应商:
@Provider
public class readerClass implements MessageBodyReader<Object>
{
@Override
public boolean isReadable(Class<?> paramClass, Type paramType,
Annotation[] paramArrayOfAnnotation, MediaType paramMediaType) {
// TODO Auto-generated method stub
return true;
}
@Override
public Object readFrom(Class<Object> paramClass, Type paramType,
Annotation[] paramArrayOfAnnotation, MediaType paramMediaType,
MultivaluedMap<String, String> paramMultivaluedMap,
InputStream paramInputStream) throws IOException,
WebApplicationException {
// TODO Auto-generated method stub
return "Successfully read from a providers reader method";
}
}
采纳答案by invariant
You misunderstood the purpose MessageBodyReader , it is used for the following purpose :
您误解了 MessageBodyReader 的用途,它用于以下目的:
Contract for a provider that supports the conversion of a stream to a Java type. To add a MessageBodyReader implementation, annotate the implementation class with @Provider. A MessageBodyReader implementation may be annotated with Consumes to restrict the media types for which it will be considered suitable
支持将流转换为 Java 类型的提供程序的合同。要添加 MessageBodyReader 实现,请使用 @Provider 注释实现类。MessageBodyReader 实现可以使用 Consumes 进行注释以限制它被认为适合的媒体类型
Example : If you have a use case where you getting come custom format other than xml/json ,you want to provide your own UnMarshaller you can use messagebody reader
示例:如果您有一个用例,您可以使用 xml/json 以外的自定义格式,您想提供自己的 UnMarshaller,您可以使用 messagebody 阅读器
@Provider
@Consumes("customformat")
public class CustomUnmarshaller implements MessageBodyReader {
@Override
public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return true;
}
@Override
public Object readFrom(Class aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap multivaluedMap, InputStream inputStream) throws IOException, WebApplicationException {
Object result = null;
try {
result = unmarshall(inputStream, aClass); // un marshall custom format to java object here
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
In webservice you can use this like ..
在网络服务中,您可以像这样使用..
@POST
@Path("/CreateAccount")
@Consumes("custom format")
public Response createAccount(@Context HttpServletRequest req,Account acc) {
saveAccount(acc); // here acc object is returned from your custom unmarshaller
return Response.ok().build();
}
More Info: Custom Marshalling/UnMarshalling Example, Jersy Entity Providers Tutorial
更多信息: 自定义编组/解组示例, Jersy 实体提供程序教程