JAX-RS writer interceptor works for every response even with NameBinding

假如想象 提交于 2020-03-03 13:00:14

问题


I need to intercept a response from the server that was generated for a specific API call and change it.
I used the JAX-RS 'WriterInterceptor' for this and I have created a NameBinding as well.
But the server is intercepting every response out from the server. What am I doing wrong here?

Shown below is the code I have tried. Why is the name binding does not work? (I have verified that when calling other API resources the particular method that I have use name binding is not called.)

Name Binding.

package com.example.common.cm.endpoint;


@Target({ElementType.TYPE, ElementType.METHOD})
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface JSONtoJWT {}

The Interceptor.

package com.example.common.cm.endpoint;


@Provider
@JSONtoJWT
public class TestInterceptor implements WriterInterceptor {

    private static final Log log = LogFactory.getLog(TestInterceptor.class);

    @Override
    public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException, WebApplicationException {

        log.info("interceptor invoked");

        OutputStream outputStream = writerInterceptorContext.getOutputStream();

        outputStream.write(("{\"message\": \"Message added in the writer interceptor in the server side\"}").getBytes());

        writerInterceptorContext.setOutputStream(outputStream);
        writerInterceptorContext.proceed();

        log.info("Proceeded");

    }
}

API Resource.

package com.example.cm.endpoint.u3.acc;

@Path("/u3/some-validation")
@Consumes({ "application/json; charset=utf-8" })
@Produces({ "application/json; charset=utf-8" })
public class SomeValidationApi  {

 @POST
    @Path("/")
    @JSONtoJWT
    @Consumes({ "application/json; charset=utf-8" })
    @Produces({ "application/json; charset=utf-8" })
    public Response someValidationPost(@ApiParam(value = "validations post" ,required=true ) SomeValidationRequestDTO someValidationConsent)
    {
        return delegate.someValidationPost(someValidationConsent);
    }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <context:property-placeholder/>
    <context:annotation-config/>
    <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/>
    <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>

<jaxrs:server id="services" address="/">
        <jaxrs:serviceBeans>

     ***Some other beans here***

            <bean class="com.example.cm.endpoint.u3.acc.SomeValidationApi/>
        </jaxrs:serviceBeans>

        <jaxrs:providers>
            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
            <bean class="com.example.common.cm.endpoint.TestInterceptor"/>
        </jaxrs:providers>
    </jaxrs:server>
</beans>

When I use the above every response from the server is intercepted and the message is added. But I want only the particular resource to invoke the Interceptor.


Also, Other than JAX-RS interceptor with writerInterceptor, are there any other good alternative to achieve this?

来源:https://stackoverflow.com/questions/57668818/jax-rs-writer-interceptor-works-for-every-response-even-with-namebinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!