Jax-RS MessageBodyReader
我正在学习MessageBodyReader方法如何从提供程序中工作。我看到该方法返回一个对象,我不知道如何从服务访问该对象。我可以获得如何从读者类返回对象的解释吗?这将有助于我为所有dto应用阅读规则。提前致谢!
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!
服务:
@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
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";
}
}
您误解了MessageBodyReader的用途,它用于以下目的:
You misunderstood the purpose MessageBodyReader , it is used for the following purpose :
支持转换流的提供商的合同到
Java类型。要添加MessageBodyReader实现,请使用@Provider注释
实现类。 MessageBodyReader
实现可以使用Consumes进行注释,以限制媒体
类型,它们被认为是合适的
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
示例:
如果您有一个用例来自xml / json以外的自定义格式,您想提供自己的UnMarshaller,您可以使用messagebody reader
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
@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();
}