How can I actually use a JAX-RS client configured with Spring? - java

My application shall make calls to a REST service. So I added the package cxf-rt-rs-client from org.apache.cxf as a dependency.
In a cxf.xml configuration this is how I define my JAX-RS client:
<jaxrs:client id="myRestClient" serviceClass="org.apache.cxf.jaxrs.client.WebClient">
<http-conf:authorization>
<sec:UserName>testuser</sec:UserName>
<sec:Password>myPassword</sec:Password>
<sec:AuthorizationType>Basic</sec:AuthorizationType>
</http-conf:authorization>
</jaxrs:client>
However I think I have missed a point. How do I actually use this client in ma Java code? There must be some mechanism which makes this myRestClient available in the application?

This is tersely documented in section Injecting proxies of CXF JAX-RS Client API documentation. To be more explicit, the useful Java code for the XML example in this section can be found in the class org.apache.cxf.systest.jaxrs.jaxws.BookStoreSoapRestImpl of CXF samples.
So, in your case, I guess something like this:
#Resource(name = "myRestClient")
private org.apache.cxf.jaxrs.client.WebClient webClient;

Related

CXF in Karaf: how to configure bean validation on subresources (preferably using Blueprint)?

I'm using Dinamics Features of CXF in Karaf and faced with the issue that Bean Validation does not work for subresources.
E.g. in the following code:
#Path("services")
public interface Service {
#Path("{id}/orders")
public Order getOrderForService(#PathParam("id") int serviceId);
}
#Path("orders")
public interface Order {
#POST
Product getProduct(#NotNull #Valid Product product);
}
when Order is root resource, bean validation works fine, but when it is invoked as a subresource of Service, bean validation does not work.
I've found an issue CXF-6297
where said that
This is not a bug - JAXRSBeanValidationInvoker can take care of it.
Note JAXRSBeanValidationInInterceptor is also a ContainerRequestFilter - so you can register it as a JAX-RS provider, but ironically, given that post-match request filters can not be applied to the locators it can not be used to validate the locators... So registering a custom invoker is the only way to go to get the subresource locators validated too
and I've red the article about Invokers
but it does not describe how to configure an Invoker using Blueprint.
So the question is: how to configure bean validation on subresources of CXF in Karaf using Blueprint?
Or may be there is another way to do that?
I really think your life would be made much easier if you used Camel CXF and Camel bean validation with camel blueprint. The learning curve for Camel is practically nothing and works wonderfully with Karaf and (camel) Blueprint. Using these two methods should resolve your issue. You can check out a sample by creating a new project from a camel-blueprint archetype. It also appears there is an archetype for camel cxf with blueprint. If you look at these options and they seems appealing I believe what you need is a dynamic router which is just a router that routes to end points based on whatever rules you give it.
https://camel.apache.org/cxf.html
https://camel.apache.org/bean-validation.html
Camel-CXF tutorial
https://camel.apache.org/better-jms-transport-for-cxf-webservice-using-apache-camel.html
Hopefully I understood your question properly and this may help.

Is it possible to setup apache-cxf without web.xml?

Is it possible to create a webservice with apache cxf (soap/rest) by "using the servlet transport without Spring and without web.xml file"?
No Its not possible.
The service will be needing the web deployment descriptor.
You can extend
CXFNonSpringJaxrsServlet for REST
and
CXFNonSpringServlet for SOAP
web-services in Apache CXF to avoid using Spring, but then you need to register them in web.xml.
You have to either use Spring configuration or web.xml.
Refer: Apache CXF - How to register a SOAP service without Spring?
It is possible. Took me quite a bit of work to figure this out for my own project and thought I'd share.
In my context, we're using OSGi HTTP Service to publish JAX-RS resources using a Jersey Servlet container and I wanted to do the same thing with CXF for JAX-WS resources.
Your class extending CXFNonSpringServlet should include the following:
private Object obj; // JAX-WS resource singleton
#Override
public void loadBus(ServletConfig conf)
{
super.loadBus(conf);
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setBus(getBus());
factory.setAddress("/some/path");
factory.setServiceBean(obj);
Server cxfServer = factory.create();
}
Note that you may load as many resources within a single servlet as you find needful. Also note that the path in factory.setAddress() is appended to the path at which you register the servlet.
Also note that I'm using the singleton pattern, rather than the handler-class pattern. I'm sure this could be modified simply to fit the other paradigm.

Restlet Client using JAX-RS annotated resources

I am writing a restlet client that will invoke some Resteasy coded rest services (cannot change the server code, hence cannot use the Restlet way of annotating resources).
Resource Interface is using JAX-RS annotations and have more than one #POST method (one of the biggest problems of Restlet when dealing with this).
I was trying to do my implementaion this way:
IAppLoginResource resource = JaxRsClientResource.createJaxRsClient("http://localhost:9090/rest", IAppLoginResource.class);
final GetLoginAppInfoResponse response = resource.getLoginAppInfo( getLoginAppInfoRequest );
The problem is that the request by default is GET, I didn't find a way to specify the request method like when using ClientResource (which I can't use because I need to deal with JaxbRepresentation and Jaxb problems).
Any sample/snippet of code that implement a Restlet client using JAX-RS annotated resources?
Any ideas?
Thanks,
I've entered an issue for this topic:
https://github.com/restlet/restlet-framework-java/issues/1081
I've tested a sample application based on your code, and it works properly using the current 2.3 branch (future 2.3.3). I wonder if the fix for this issue https://github.com/restlet/restlet-framework-java/issues/1072 helps.
Regarding the documentation, I 'll complete the current page (http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/jaxrs), cf this issue: https://github.com/restlet/restlet-framework-java/issues/1084.
You can also have a look at the org.restlet.test project, especially in this package https://github.com/restlet/restlet-framework-java/tree/2.3/modules/org.restlet.test/src/org/restlet/test/ext/jaxrs.

Naming Spring Services when accessed by different client types

I am quite new to Spring and have a question related to Spring Service naming convention.
I have written a service, and used an annotation to define and name it.
#Service(value="CustomerService")
This service is implemented within a library that is used by a web app. Everything works fine and I can autowire my service into my client components.
Now I would also like to expose this service using http invoker. This works ok. I have define a /CustomerService http service which accesses CustomerService bean.
The issue I have is that one of my components, a client side component, that I used in my web app (CustomerDetailsValidator) can also be used in this new application.
In my CustomerDetailsValidator I have something like this:
#Autowired
#Qualifier(name="CustomerService").
But if I want to reuse my CustomerDetailsValidator and use it in my new app, this time I need to wire it to the httpservice instead.
Which means that the #Autowired and #Qualifier code is useless.
My question is what is the best practice in this case?
Should I still use #Service?
I guess I cannot use Qualifier anymore.
My feeling is that I should define everything in xml in each application context.
The web app using the library directly would just use the CustomerService bean as a singleton.
While my new client app would link the customer service id to the http service.
Is that a good approach? Do we have patterns for this?
Thanks and regards
Gilles

Can I use a SimpleJaxWsServiceExporter with a javax.servlet.Filter?

I would like to set up a webservice endpoint using Spring's SimpleJaxWsServiceExporter for ease of use, but I also need to protect the webservice using a subclass of Jespa's HttpSecurityFilter
I suspect this does not work out of the box as SimpleJaxWsServiceExporter is using a separate HTTP server to the host webapp containing the filter - how should I accomplish this?
I have a class annotated with #WebService and in my applicationContext.xml
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"
p:baseAddress="http://localhost:9581/"/>
I found a couple of alternative ways to host the endpoint within the same HTTP server (behind the filter)
Either using JAX-WS WSSpringServlet and registering my #WebService beans via the wss:binding xml tag under the http://jax-ws.dev.java.net/spring/servlet namespace, or my preferred option, as JAX-WS Spring integration has Maven dependencies on Spring 2, using CXF.

Categories

Resources