Jersey 2.0 SpringBoot客户端过滤器自动接线失败 - nullpointerexception

时间:2016-09-01 22:09:28

标签: spring-boot jersey

我已按照文档使用Springboot配置Jersey 2.0。

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        super();
        register(AccountResource.class);
        register(CustomerAccountResource.class);
     //   register(new ClientLoggingFilter());
        register(new ServerLoggingFilter());
        register(HapiExceptionMapper.class);
        register(ConstraintViolationExceptionMapper.class);
        register(GeneralExceptionMapper.class);
        property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    }
}

我正在注册两个过滤器,

@Component
@PreMatching
@Priority(Integer.MIN_VALUE)
public class ServerLoggingFilter implements ContainerRequestFilter, ContainerResponseFilter{

@Component
public class ClientLoggingFilter implements ClientRequestFilter, ClientResponseFilter{

我在我的应用程序中使用Jersey Server和Jersey Client。

我已将Jersey客户端配置为bean,因此只有一个实例。

@Configuration
public Class XClient {

@Bean
public Client client() {
return ClientBuilder
                .newBuilder()
                .sslContext(sslContext)
                .build()
                .register(new ClientLoggingFilter());
}
}

当我在ClientLoggingFilter中自动装配另一个Spring托管Bean时,我得到一个空指针异常。同一个bean在ServerLoggingFilter中运行良好。我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

想出来,

@Configuration
public Class XClient {

@Autowired
ClientLoggingFilter loggingFilter;

@Bean
public Client client() {
return ClientBuilder
                .newBuilder()
                .sslContext(sslContext)
                .build()
                .register(loggingFilter);
}
}

必须在配置类中自动装配过滤器。