admin管理员组文章数量:1395352
I am developing a JAX-RS (CXF) API where I expect an XML response when requesting AppInfo. However, the response is
No message body writer has been found for class en.aas.inventoryapp.model.AppInfo, ContentType: application/json
, despite ensuring proper configurations.
Issue Details:
I have defined a REST API method in AppInfoResource.java
:
@GET
@Produces({
MediaType.APPLICATION_JSON, MediaType.TEXT_XML
})
@Path("/appinfo")
public synchronized AppInfo getXml() {
AppInfo info = new AppInfo();
info.populate();
return info;
}
The AppInfo model is annotated with @XmlRootElement
to support XML serialization:
@XmlRootElement
public class AppInfo {
private String appName;
private String appServerName;
private String appServerVersion;
private String dbServerName;
private String dbName;
public String getAppName() { return appName; }
public void setAppName(String appName) { this.appName = appName; }
public String getAppServerName() { return appServerName; }
public void setAppServerName(String name) { this.appServerName = name; }
public String getAppServerVersion() { return appServerVersion; }
public void setAppServerVersion(String version) { this.appServerVersion = version; }
public String getDbServerName() { return dbServerName; }
public void setDbServerName(String dbServerName) { this.dbServerName = dbServerName; }
public String getDbName() { return dbName; }
public void setDbName(String dbName) { this.dbName = dbName; }
public void populate() {
this.appName = InventoryApp.getProp(InventoryApp.MODULE_NAME);
this.appServerName = InventoryApp.getProp(InventoryApp.APP_SERVER_NAME);
this.appServerVersion = InventoryApp.getProp(InventoryApp.SERVERVERSION);
this.dbServerName = InventoryApp.getProp(InventoryApp.SERVER_NAME);
this.dbName = InventoryApp.getProp(InventoryApp.DBNAME);
}
}
The CXF configuration is as follows:
@Configuration
public class CxfConfig {
@Bean
public JacksonJsonProvider jacksonJsonProvider() {
return new JacksonJsonProvider();
}
@Bean
public JAXBElementProvider<?> jaxbElementProvider() {
return new JAXBElementProvider<>();
}
@Bean
public JAXRSServerFactoryBean jaxRsServer() {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setResourceClasses(
AuthService.class,
DealerService.class,
InventoryService.class
);
factoryBean.setAddress("/rest");
factoryBean.setProviders(List.of(new JacksonJsonProvider(), new JAXBElementProvider<>()));
return factoryBean;
}
}
Expected XML Response:
<AppInfo>
<appName>InventoryApp</appName>
<appServerName>Server1</appServerName>
<appServerVersion>1.0</appServerVersion>
<dbServerName>DBServer1</dbServerName>
<dbName>InventoryDB</dbName>
</AppInfo>
Actual Response:
No message body writer has been found for class en.aas.inventoryapp.model.AppInfo, ContentType: application/json
How can I ensure CXF correctly serializes my object into XML?
本文标签: javaCXF JAXRS Endpoint Not Returning XML Response for AppInfoStack Overflow
版权声明:本文标题:java - CXF JAX-RS Endpoint Not Returning XML Response for AppInfo - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107180a2591119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论