CXF Restful web service with generic payload - rest

Is there a way in CXF to implement a Restful webservice which will accept different xml requests using one Web service method?
e.g. Can one create a Restful endpoint to accept this type of XML through one web service method?
<Data>
<Book>BN1</Book>
</Data>
& this too using same web service method?
<Data>
<Disk>DN1</Disk>
</Data>

I think this post: Apache CXF: Consume XML POST payload... shows a good example of how to declare a CXF REST service as receiving POST XML data.
For your example of handling different XML content, instead of 'Bean' in the above you'd have an #XmlRootElement that's the Data, with a child that's a #XmlAnyElement.

Ok so I am using this for generic XML.
public interface Callback {
#POST
#Path("/submit")
#Consumes("text/xml")
#Produces("application/xml")
public Response submit(String incomingXML);
}
Basically I am getting whole xml as a string in my method body, As CXF is not parsing it, it can remain generic.

Related

Jersey REST (GET) throwing MessageBodyWriter not found for media type=application/xml

I have developed a REST service (GET) using Jersey. I want to return the response as application/xml.
I get an exception when I annotate the following whereas when I change it to MediaType.APPLICATION_JSON, service works and returns the response as JSON.
#GET
#Produces(MediaType.APPLICATION_XML)
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml
Many blogs have advised to include a dependency to jersey-media-jaxb but I have another service which is successfully returning XML response without that dependency. The only difference is that service is a POST end-point whereas the one which is currently not working is a GET end-point.
If the annotation #XmlRootElement(name = "name") not mentioned in your class, try to add it. it worked for me!

How to call soap web service from camel rest java DEL

I am trying to call soap web service from camel rest, from java DSL. but getting server error with 500 response code.
I will receive call from a rest with json data and i have to make call to a third party soap service also i need to process the soap response and send back the response in json formate.
here is my code
{
String getCustomerDetailsurl="http://<serverip>/webservice/Service.asmx?op=GetClientDetail&bridgeEndpoint=true";
rest("/customers")
.description("Aviva Mobile sales customer service")
.consumes("application/json")
.produces("application/json")
.post().type(ClientRequest.class) // incomming request data
.route()
.from("direct:start")
//.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.process(new CustomerProcessor()).marshal().xstream()
.to(getCustomerDetailsurl);
Error
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://<serverip>/webservice/Service.asmx?op=GetClientDetail with statusCode: 500
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:239) ~[camel-http-2.17.5.jar:2.17.5]
at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:161) ~[camel-http-2.17.5.jar:2.17.5]
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468) ~[camel-core-2.17.5.jar:2.17.5]
}
You have to look at Server side for what actual error is.
You may use some tool like tcp monitor (tcpMon) to see exactly what you send and what you get back.
Actually, To use SOAP service it is much better to use SOAP client, rather than raw call through http. Take a look at Camel-CXF component. Then create a CXF endpoint and use it uri in your .to({cxfEndpointUri}). CXF will do all SOAP work for you. Maybe you will need to make a little work in its interceptors, like authorization if Server requires it.
P.S. In your code what kind of Exchange.body your CustomerProcessor produces? is it a valid for server SOAP Envelope? does it have all what server requires by its contract (WSDL)?

Can I get request header and response header for web service function?

I am using Java with Apache CXF to write the backend for AngularJS (single-page web site). In my REST service, I need access to the header of the http request (i.e. parameters and cookies) and I need access to the response header also (i.e. parameters and cookies). The main reason for this is security, authentication purposes and session management. Those are important reasons.
Is there a way of getting both of these structures in Apach CXF RESTfull code?
You can inject request by using #Context [javax.ws.rs.core.Context]
public Response myRest(#Context HttpServletRequest request /*, other parameters if you have like #QueryParam */ ){
request.getCookies();
request.getUserPrincipal();
}
You can set cookie or header in response as bellow
ResponseBuilder builder = Response.ok(); //Response.status(500) either way
builder.cookie(arg0);
builder.header(arg0, arg1);
return bulider.build();

Create WSDL message from SOAP Response

I have a WSDL that contains a different response then the actual responds with. I want to update the WSDL with the format of the API response but am having trouble. Is there a tool to generate a WSDL response from SOAP XML?
I should mention that they gave me the WSDL as a file, no public WSDL is visible.
The response contains this unknown anonymous xml element, s-gensym3. It should be userid.
If it's a .NET web service you can browse to [serviceurl]?WSDL to get the WSDL.
There's surely service endpoint you communicate with. You shall be able to invoke this endpoint in the browser and add to the end:
?wsdl
i.e. http://service:8123/operationName?wsdl
This will print you entire WSDL. Just make sure you have the same and you're done.

Upload data through restful service

I want to write a RESTful web service for client to upload data.
The data format is JSON
But I don't know much about it,can you give some sample code in JAVA? include the code of service and client that could demonstrate me the whole process of data uploading.
A good place to start is the Jackson Tutorials. Then look at either Jersey's JSON Support or RESTeasy's JSON Support depending on which framework you happen to be using. Data uploading is a open-ended topic since there are a number of different ways that it can be accomplished. If you POST JSON directly to the service then you can use JAXRS annotations like:
#Path("/myservice")
public class MyService {
#POST #Consumes("application/mytype+json")
public Response processPostRequest (JsonBeanType postData) {
...
}
}
The processPostRequest method will be invoked whenever a client POSTs data that includes the Content-Type: application/mytype+json HTTP header to the /myservice resource.
Another way to upload data is to send it using an HTML form. There are a bunch of examples of processing HTML forms in Java. The SO question How can I handle multipart form data post requests in my Java servlet should start you off in the right direction.