gpt4 book ai didi

java - Apache CXF (JAX-RS) 与 Spring Javaconfig 和 Jackson

转载 作者:行者123 更新时间:2023-11-30 06:33:55 25 4
gpt4 key购买 nike

如何使用 JAX-RS Spring Javaconfig 和 Jackson 设置 Apache CXF?我读过了 http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarterhttp://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-Jackson

建议使用

<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
</jaxrs:providers>

但我想远离 XML 并使用纯 javaconfig。

我当前的配置是

@SpringBootApplication
public class Application {
@Autowired
private Bus bus;

public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}

@Bean
public Server rsServer() {
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setBus(bus);
endpoint.setAddress("/");
endpoint.setServiceBeans(Arrays.<Object>asList(new MyService()));

return endpoint.create();
}
}

在我的类 MyService 中定义的 Andpoint 可以工作,但是当调用他时我收到消息

org.apache.cxf.jaxrs.utils.JAXRSUtils : No message body writer has been found for class MyServiceResponse, ContentType: application/json;charset=UTF-8

最佳答案

您需要添加一个 json Provider bean。这将充当消息提供者。您的应用程序类应该如下所示。

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider

@SpringBootApplication
public class Application {
@Autowired
private Bus bus;

public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}

@Bean
public Server rsServer() {
List<? extends Object> providers = new ArrayList<>();
providers.add(getJsonProvider());
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setProviders(providers);
endpoint.setBus(bus);
endpoint.setAddress("/");
endpoint.setServiceBeans(Arrays.asList(new MyService()));

return endpoint.create();
}

@Bean
public JacksonJsonProvider getJsonProvider() {
new JacksonJsonProvider();
}
}

关于java - Apache CXF (JAX-RS) 与 Spring Javaconfig 和 Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45541765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com