CXF @PUT请求“未找到消息正文编写器”异常

时间:2016-01-23 18:59:44

标签: spring cxf junit4 cxf-client

我完全卡住了!花了很多时间没有进展......

我有一个带有CXF 3(3.1.4)的Spring 4(4.2.3.RELEASE)应用程序,我正在尝试JUnit测试。除了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:application / xml的消息正文编写器         at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:780)         在org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:494)         在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 );

端点impl:

@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;

    }

}

首先,我如何确保fastxml Jackson 2提供程序实际上是序列化邮件正文?它没有在堆栈跟踪中看到任何杰克逊,但我确实在提供商中设置了它。

我找到了这个链接来演示一个自定义的ContextProvider,这是实现这个工作的唯一方法吗?似乎完全多余...... http://www.blackpepper.co.uk/custom-context-providers-for-cxf-with-the-context-annotation/

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

... GEEEEEEZZZ

感觉就像一个笨蛋,忘记将相同的Jackson序列化器(提供者)添加到客户端。再看看堆栈跟踪,我注意到这些方法只来自客户端,所以显然客户端不知道如何使用我扔掉的POJO ......

更新了测试代码:

github