You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Rice Yeh <ri...@gmail.com> on 2020/01/28 07:38:45 UTC

[JAXRS] Customize the response for no operation found

Hi
  I like to forward the request to some resource not managed by cxf servlet
when no class method (operation) is found for some resource class. I try to
do it with outFaultInterceptor and register it like below. But the
interceptor is never called. Is there any example?

<jaxrs:outFaultInterceptors>
   <ref bean="outFault"/>
</jaxrs:outFaultInterceptors>

Best regards,
Rice

Re: [JAXRS] Customize the response for no operation found

Posted by Rice Yeh <ri...@gmail.com>.
Hi Andy,
  Thanks, Andy. Your solution is simple but  a good fit to my problem.

Regards,Rice

On Wed, Jan 29, 2020 at 6:46 AM Andy McCright <j....@gmail.com>
wrote:

> I have not tested this myself, but I was wondering whether an
> ExceptionMapper<NotFoundException> might work?
>
> Something like:
>
> @Provider
> public class ForwardIfNotFoundExceptionMapper implements
> ExceptionMapper<NotFoundException> {
>
>     @Context
>     UriInfo uriInfo;
>
>     @Context
>     Request request;
>
>     @Override
>     Response toResponse(NotFoundException ex) {
>         Client c = ClientBuilder.newClient();
>         return
> c.target(uriInfo.getRequestUri()).request().method(request.getMethod())
>     }
> }
>
> You might also need to "copy" the headers from the request into the client
> request.
>
> Hope this helps,
>
> Andy
>
> On Tue, Jan 28, 2020 at 6:51 AM RobCodes <te...@gmail.com> wrote:
>
> > Hi Rice,
> >
> > I recently figured out how to extend for a custom class as well. I think
> > you
> > will find this useful though I can't speak to actually forwarding the
> > request. I chose the annotation approach but there are other ways to do
> it.
> > Please see:
> >
> > https://cxf.apache.org/docs/interceptors.html
> > and
> > https://cxf.apache.org/docs/configuration.html
> >
> > Excerpts from the first page referenced above:
> > ----------------------
> > You can also use annotation to add the interceptors from the SEI or
> service
> > class. When CXF create the server or client, CXF will add the interceptor
> > according with the annotation.
> >
> > @org.apache.cxf.interceptor.InInterceptors (interceptors =
> > {"com.example.Test1Interceptor" })
> > @org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
> > {"com.example.Test2Interceptor" })
> > @org.apache.cxf.interceptor.OutInterceptors (interceptors =
> > {"com.example.Test1Interceptor" })
> > @org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
> > {"com.example.Test2Interceptor","com.example.Test3Intercetpor" })
> > @WebService(endpointInterface =
> > "org.apache.cxf.javascript.fortest.SimpleDocLitBare",
> >             targetNamespace = "uri:org.apache.cxf.javascript.fortest")
> > public class SayHiImplementation implements SayHi {
> >    public long sayHi(long arg) {
> >        return arg;
> >    }
> >    ...
> > }
> >
> > ----------------
> >
> > Adding MyInterceptor to the bus:
> >
> > <beans xmlns="http://www.springframework.org/schema/beans"
> >        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >        xmlns:cxf="http://cxf.apache.org/core"
> >        xsi:schemaLocation="
> > http://www.springframework.org/schema/beans
> > http://www.springframework.org/schema/beans/spring-beans.xsd
> > http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
> >
> >     <bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
> >
> >
> >
> >     <cxf:bus>
> >         <cxf:inInterceptors>
> >             <ref bean="MyInterceptor"/>
> >         </cxf:inInterceptors>
> >         <cxf:outInterceptors>
> >             <ref bean="MyInterceptor"/>
> >        </cxf:outInterceptors>
> >     </cxf:bus>
> > </beans>
> > ---------------------
> >
> > Hope this helps while waiting a response from the CXF team.
> >
> >
> >
> > -----
> > Regards,
> > RobCodes
> > --
> > Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html
> >
>

Re: [JAXRS] Customize the response for no operation found

Posted by Andy McCright <j....@gmail.com>.
I have not tested this myself, but I was wondering whether an
ExceptionMapper<NotFoundException> might work?

Something like:

@Provider
public class ForwardIfNotFoundExceptionMapper implements
ExceptionMapper<NotFoundException> {

    @Context
    UriInfo uriInfo;

    @Context
    Request request;

    @Override
    Response toResponse(NotFoundException ex) {
        Client c = ClientBuilder.newClient();
        return
c.target(uriInfo.getRequestUri()).request().method(request.getMethod())
    }
}

You might also need to "copy" the headers from the request into the client
request.

Hope this helps,

Andy

On Tue, Jan 28, 2020 at 6:51 AM RobCodes <te...@gmail.com> wrote:

> Hi Rice,
>
> I recently figured out how to extend for a custom class as well. I think
> you
> will find this useful though I can't speak to actually forwarding the
> request. I chose the annotation approach but there are other ways to do it.
> Please see:
>
> https://cxf.apache.org/docs/interceptors.html
> and
> https://cxf.apache.org/docs/configuration.html
>
> Excerpts from the first page referenced above:
> ----------------------
> You can also use annotation to add the interceptors from the SEI or service
> class. When CXF create the server or client, CXF will add the interceptor
> according with the annotation.
>
> @org.apache.cxf.interceptor.InInterceptors (interceptors =
> {"com.example.Test1Interceptor" })
> @org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
> {"com.example.Test2Interceptor" })
> @org.apache.cxf.interceptor.OutInterceptors (interceptors =
> {"com.example.Test1Interceptor" })
> @org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
> {"com.example.Test2Interceptor","com.example.Test3Intercetpor" })
> @WebService(endpointInterface =
> "org.apache.cxf.javascript.fortest.SimpleDocLitBare",
>             targetNamespace = "uri:org.apache.cxf.javascript.fortest")
> public class SayHiImplementation implements SayHi {
>    public long sayHi(long arg) {
>        return arg;
>    }
>    ...
> }
>
> ----------------
>
> Adding MyInterceptor to the bus:
>
> <beans xmlns="http://www.springframework.org/schema/beans"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xmlns:cxf="http://cxf.apache.org/core"
>        xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
> http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
>
>     <bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
>
>
>
>     <cxf:bus>
>         <cxf:inInterceptors>
>             <ref bean="MyInterceptor"/>
>         </cxf:inInterceptors>
>         <cxf:outInterceptors>
>             <ref bean="MyInterceptor"/>
>        </cxf:outInterceptors>
>     </cxf:bus>
> </beans>
> ---------------------
>
> Hope this helps while waiting a response from the CXF team.
>
>
>
> -----
> Regards,
> RobCodes
> --
> Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html
>

Re: [JAXRS] Customize the response for no operation found

Posted by RobCodes <te...@gmail.com>.
Hi Rice,

I recently figured out how to extend for a custom class as well. I think you
will find this useful though I can't speak to actually forwarding the
request. I chose the annotation approach but there are other ways to do it.
Please see: 

https://cxf.apache.org/docs/interceptors.html
and
https://cxf.apache.org/docs/configuration.html

Excerpts from the first page referenced above:
----------------------
You can also use annotation to add the interceptors from the SEI or service
class. When CXF create the server or client, CXF will add the interceptor
according with the annotation.

@org.apache.cxf.interceptor.InInterceptors (interceptors =
{"com.example.Test1Interceptor" })
@org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
{"com.example.Test2Interceptor" })
@org.apache.cxf.interceptor.OutInterceptors (interceptors =
{"com.example.Test1Interceptor" })
@org.apache.cxf.interceptor.InFaultInterceptors (interceptors =
{"com.example.Test2Interceptor","com.example.Test3Intercetpor" })
@WebService(endpointInterface =
"org.apache.cxf.javascript.fortest.SimpleDocLitBare",
            targetNamespace = "uri:org.apache.cxf.javascript.fortest")
public class SayHiImplementation implements SayHi {
   public long sayHi(long arg) {
       return arg;
   }
   ...
}

----------------

Adding MyInterceptor to the bus:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://cxf.apache.org/core"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
 
    <bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
 
    
 
    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="MyInterceptor"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="MyInterceptor"/>
       </cxf:outInterceptors>
    </cxf:bus>
</beans>
---------------------

Hope this helps while waiting a response from the CXF team.



-----
Regards,
RobCodes
--
Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html