我必须使用CXF 2.7.11实现REST服务,该服务已经实现了WS-SOAP服务。
我收到错误"找不到请求类MyRequest,ContentType:application / json"的消息正文阅读器。
我正在尝试以下实施:
@Path("/myService")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MyServiceImpl implements MyService {
@Override
@POST
@Path("/myOperation")
public MyResponse myOperation(MyRequest request){
// my implementation
}
但我收到以下错误:
8-feb-2018 17.12.15 org.apache.cxf.jaxrs.utils.JAXRSUtils readFromMessageBody
WARNING: No message body reader has been found for request class MyRequest, ContentType : application/json.
8-feb-2018 17.12.15 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.WebApplicationException
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1231)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:756)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:715)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:252)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:88)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:206)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:162)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:201)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:178)
at it.prometeia.hnwi.searchengine.configuration.filter.CustomMonitoringFilter.doFilter(CustomMonitoringFilter.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:695)
我该如何解决?
答案 0 :(得分:0)
错误表示CXF中未注册MessageBodyReader
。
由于我没有使用Spring而我没有ApplicationContext,所以我解决了以下问题:
private void createService(Class<?> serviceClass, String address, Object serviceImpl) {
JAXRSServerFactoryBean srvFactory = new JAXRSServerFactoryBean();
srvFactory.setServiceClass(serviceClass);
srvFactory.setAddress("/rest"+address);
srvFactory.setServiceBean(serviceImpl);
srvFactory.getInInterceptors().add(new CustomInInterceptor());
srvFactory.getOutInterceptors().add(new CustomOutInterceptor());
srvFactory.setProvider(new JacksonJsonProvider());
srvFactory.create();
}
我需要注册一个提供者,我使用了JacksonJsonProvider
,它就像一个魅力。
答案 1 :(得分:0)
我不确定这个解决方案是否适合所有人,但我就是这样解决的。
我的 MyRequest
课程最初是这样的:
@Data
public class MyRequest {
private String attribute1;
private String attribute2;
}
@Data
是一个 Lombok 注释,它为类提供 Getter、Setter 和 toString 方法。
我用这些注释更新了 MyRequest
类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "myRequest")
public class MyRequest {
private String attribute1;
private String attribute2;
}
也就是说,我在类中添加了以下 3 个新注释,并解决了该问题。
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@NoArgsConstructor 是一个 Lombok 注释,用于创建无参数构造函数。
@AllArgsConstructor 是一个 Lombok 注释,它创建一个全参数构造函数。
@XmlRootElement 来自 javax.xml.bind 包,它导致在模式中生成全局元素声明。全局元素声明与类映射到的 XML 架构类型相关联。
虽然我没有时间将 3 个注释中的每一个逐一隔离以查看哪一个实际上解决了此问题,但我的直觉是添加 @XmlRootElement
注释可能已为我解决了此问题。< /p>