我有一个REST端点接口,如下所示:
@POST
@Path("/")
@Consumes(MediaType.TEXT_PLAIN)
@Produces({ MediaType.APPLICATION_XML })
MyResponse performAction(MyRequest request);MyRequest类应该使用自定义MessageBodyReader创建,我试图这样做:
@Provider
@Consumes(MediaType.TEXT_PLAIN)
public class MyRequestBodyReader implements MessageBodyReader<MyRequest > {
@Override
public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType) {
System.out.println("### we are gonna in the equality");
return type == MyRequest.class;
}
@Override
public MyRequest readFrom(final Class<MyRequest> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException {
System.out.println("unmarshalling");
return JAXB.unmarshal(entityStream, MyRequest.class);
}
}但是,当我将我的应用程序部署到Jboss时,我会得到一个错误,说明:
Could not find message body reader for type: com.some.MyRequest of content type: text/plain为了尝试查看依赖关系是否是一个问题,仅为了起见,我将几乎所有resteasy引用添加到我的pom中,如下所示:
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-resteasy</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>2.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jsapi</artifactId>
<version>2.3.6.Final</version>
<scope>provided</scope>
</dependency>所有这些依赖项都在我的jboss模块文件夹中。我是否在依赖部分或配置中遗漏了一些东西,以便获取MessageBodyReader?
看看docs 这里,就会发现:
Resteasy ServletContextLoader将自动扫描web.xml /lib和类目录中带有@Provider注释的类,或者您可以在web.xml中手动配置它们。见安装/配置
我已经检查了生成的war文件,并且能够确认WEB目录包含一个lib文件夹,并且我的jars (其中一个包含提供程序)实际上是存在的。然而,自动扫描似乎并没有把它捡起来。
我尝试了姆基永的解决方案,但这并没有为我解决问题。
我使用了从文档到甚至试图正确调用消息体编写器的方法。
我尝试过制作一个web.xml文件,其内容如下,但没有成功。
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.some.MyRequestBodyReader</param-value>
</context-param>
</web-app>我也尝试过解析堆栈溢出,但似乎大多数与作者相关的问题都与缺少的库有关,在这些库中没有自定义编写器,与此一类似。
发布于 2016-12-27 18:44:26
看起来像开关码和jboss版本组合,我使用的是支持当前的@Provider注释。如果我能想出一个自定义的解决方案,我将更新答案。
https://stackoverflow.com/questions/41305705
复制相似问题