
[英]Apache CXF as Client for restful API - No message body writer has been found for class
[英]CXF @PUT request “No message body writer has been found” exception
我完全被卡住了! 花了很多时间没有进展...
我有一个尝试使用JUnit测试的带有CXF 3(3.1.4)的Spring 4(4.2.3.RELEASE)应用程序。 除了PUT请求之外,其他所有东西都运行良好。 我收到以下错误:
Caused by: org.apache.cxf.interceptor.Fault: No message body writer has been found for class com.someproject.logic.api.data.User, ContentType: application/xml
at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1222)
at org.apache.cxf.jaxrs.client.AbstractClient$AbstractBodyWriter.handleMessage(AbstractClient.java:1091)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:649)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1093)
... 49 more
原因:javax.ws.rs.ProcessingException:找不到类com.someproject.logic.api.data.User的消息正文编写器,ContentType:org.apache.cxf.jaxrs.client.AbstractClient上的application / xml。 org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:494)处的reportMessageHandlerProblem(AbstractClient.java:780)org.apache.cxf.jaxrs.client.WebClient $ BodyWriter.doWriteBody(WebClient.java: 1217)...还有53个
我还尝试了“ application / json”,并得到了相同的结果。
这是CXF配置:
@Bean
@DependsOn("cxf")
public Server jaxRsServer() {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
List<Object> providers = new ArrayList<Object>();
// get all the class annotated with @JaxrsService
List<Object> beans = configUtil.findBeans(JaxrsService.class);
if (beans.size() > 0) {
// add all the CXF service classes into the CXF stack
sf.setServiceBeans(beans);
sf.setAddress("http://localhost:8080/api");
sf.setBus(springBus);
sf.setStart(true);
// set JSON as the response serializer
JacksonJsonProvider provider = new JacksonJsonProvider();
providers.add( provider );
}
// add custom providers if any
sf.setProviders(providers);
return sf.create();
}
终点:
@Path("/user")
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, })
@Produces( MediaType.APPLICATION_JSON )
public User updateUser( User user );
端点展示:
@Override
public User updateUser( User user ) {
System.out.println( "updateUser: " + user );
return user;
}
测试:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
webClient = WebClient.create( "http://localhost:8080/api" );
WebClient.getConfig( webClient ).getRequestContext().put( LocalConduit.DIRECT_DISPATCH, Boolean.TRUE );
webClient.accept( "application/json" );
}
@Test
public void testPut() {
String apiUrl = "/user";
webClient.path( apiUrl );
User user = createDummyAPIUser();
try {
System.out.println( "testUpdateUser: PUT request to " + apiUrl );
String response = webClient.type( MediaType.APPLICATION_JSON ).put( user, String.class );
System.out.println( "testUpdateUser: " + apiUrl + " response: " + response );
//updatedUser = (new ObjectMapper()).readValue( response, User.class );
} catch( Exception e ) {
e.printStackTrace();
fail();
return;
}
}
首先,我如何确保fasterxml Jackson 2提供程序实际上在序列化消息正文? 它在堆栈跟踪中看不到任何Jackson,但我在提供程序中进行了设置。
我已经找到此链接来演示自定义ContextProvider,这是使此工作正常的唯一方法吗? 似乎完全多余... http://www.blackpepper.co.uk/custom-context-providers-for-cxf-with-the-context-annotation/
有任何想法吗?
谢谢!!
GEEEEEEZZZ ...
感觉像个傻瓜,忘了向客户端添加相同的Jackson序列化器(provider)。 再次查看stacktrace时,我注意到这些方法仅来自客户端,因此显然客户端不知道如何使用我抛出的POJO。
更新的测试代码:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final List<Object> providers = new ArrayList<Object>();
JacksonJaxbJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider();
providers.add( jacksonJsonProvider );
webClient = WebClient.create( "http://localhost:8080/api", providers );
WebClient.getConfig( webClient ).getRequestContext().put( LocalConduit.DIRECT_DISPATCH, Boolean.TRUE );
webClient.accept( "application/json" );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.