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