简体   繁体   中英

How to use javax.ws.rs.core.Feature with CXF?

I would like to use a JAX-RS Feature I created with CXF.

I would prefer to use a JAX-RS Feature ( javax.ws.rs.core.Feature ) if possible and not a CXF Feature ( org.apache.cxf.feature.Feature ). I would also prefer to use SpringComponentScanServer ( org.apache.cxf.jaxrs.spring.SpringComponentScanServer ) to configure CXF rather than having to create the server factory or servers manually.

Here is how I tried to configure CXF:

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.mycustomapp.restexception.providers.RestExceptionFeature;
import org.apache.cxf.jaxrs.spring.SpringComponentScanServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(SpringComponentScanServer.class)
public class CxfConfiguration
{
    @Bean
    public RestExceptionFeature restExceptionFeature()
    {
        return new RestExceptionFeature();
    }

    @Bean
    public JacksonJsonProvider jacksonJsonProvider()
    {
        return new JacksonJsonProvider();
    }
}

Here is my custom feature:

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

@Provider
public class RestExceptionFeature implements Feature
{
    @Override
    public boolean configure(FeatureContext context)
    {
        context.register(RestExceptionBodyReader.class);
        context.register(RestExceptionMapper.class);
        context.register(RestExceptionCxfClientMapper.class);

        return true;
    }
}

RestExceptionFeature.configure() is never called.

Created ticket https://issues.apache.org/jira/browse/CXF-6879 My code should work in CXF 3.1.7 when it is released.

It is working now if I use CXF 3.1.7-SNAPSHOT or CXF 3.2.0-SNAPSHOT from http://repository.apache.org/snapshots/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM