如何将自定义 MessageBodyWriter 应用于对象列表?
How to apply a custom MessageBodyWriter to lists of objects?
在 Dropwizard 网络服务中,我想使用以下自定义 MessageBodyWriter
.
return 数据库中 class Test
的对象
@Provider
@Produces("application/Test")
public class TestMarshaller implements MessageBodyWriter<Test>{
public long getSize(Test obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == Test.class;
}
public void writeTo(Test obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
httpHeaders.add("Content-Type", "text/plain; charset=UTF-8");
StringBuffer str = new StringBuffer();
str.append(obj.getData());
outputStream.write(str.toString().getBytes());
}
}
这适用于使用以下方法的单个对象
@GET
@Produces("application/Test")
@Path("/{ID}")
public Test getTest(@PathParam(value = "ID") LongParam ID) {
Test result = Test.findInDB(ID.get());
return result;
}
现在我想对元素列表使用相同的 MessageBodyWriter
。
@GET
@Produces("application/Test")
@Path("/{ID}")
public List<Test> getTest(@PathParam(value = "ID") String IDs) {
List<Test> listTest = Test.findMultiInDB(IDs);
return listTest;
}
导致错误
org.glassfish.jersey.message.internal.WriterInterceptorExecutor: MessageBodyWriter not found for media type=application/Test, type=class java.util.ArrayList, genericType=java.util.List.
A search 建议使用 GenericEntity
。
@GET
@Produces("application/Test")
@Path("/{ID}")
public Response getTest(@PathParam(value = "ID") String IDs) {
List<Test> listTest = Test.findMultiInDB(IDs);
GenericEntity<List<Test>> result = new GenericEntity<List<Test>>(listTest) {};
return Response.ok(result).build();
}
不幸的是,它导致了与上面完全相同的错误。
我的问题是我需要更改什么才能使其正常工作,或者是否需要另一个 MessageBodyWriter
来处理列表?
错误消息很清楚,Jersey 寻找类型为 java.util.List
的 MessageBodyWriter
,但只找到 MessageBodyWriter<Test>
,因此要么为列表 MessageBodyWriter<java.util.List>
创建一个新的或使用 Object
并在对象 class 上使用 if else。
在 Dropwizard 网络服务中,我想使用以下自定义 MessageBodyWriter
.
Test
的对象
@Provider
@Produces("application/Test")
public class TestMarshaller implements MessageBodyWriter<Test>{
public long getSize(Test obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == Test.class;
}
public void writeTo(Test obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
httpHeaders.add("Content-Type", "text/plain; charset=UTF-8");
StringBuffer str = new StringBuffer();
str.append(obj.getData());
outputStream.write(str.toString().getBytes());
}
}
这适用于使用以下方法的单个对象
@GET
@Produces("application/Test")
@Path("/{ID}")
public Test getTest(@PathParam(value = "ID") LongParam ID) {
Test result = Test.findInDB(ID.get());
return result;
}
现在我想对元素列表使用相同的 MessageBodyWriter
。
@GET
@Produces("application/Test")
@Path("/{ID}")
public List<Test> getTest(@PathParam(value = "ID") String IDs) {
List<Test> listTest = Test.findMultiInDB(IDs);
return listTest;
}
导致错误
org.glassfish.jersey.message.internal.WriterInterceptorExecutor: MessageBodyWriter not found for media type=application/Test, type=class java.util.ArrayList, genericType=java.util.List.
A search 建议使用 GenericEntity
。
@GET
@Produces("application/Test")
@Path("/{ID}")
public Response getTest(@PathParam(value = "ID") String IDs) {
List<Test> listTest = Test.findMultiInDB(IDs);
GenericEntity<List<Test>> result = new GenericEntity<List<Test>>(listTest) {};
return Response.ok(result).build();
}
不幸的是,它导致了与上面完全相同的错误。
我的问题是我需要更改什么才能使其正常工作,或者是否需要另一个 MessageBodyWriter
来处理列表?
错误消息很清楚,Jersey 寻找类型为 java.util.List
的 MessageBodyWriter
,但只找到 MessageBodyWriter<Test>
,因此要么为列表 MessageBodyWriter<java.util.List>
创建一个新的或使用 Object
并在对象 class 上使用 if else。