I am trying to write a cxf interceptor which will forward all the incoming requests from my app to another app. However for POST requests I am unable to get the body of the request.
The code I am using looks like :
String body = message.getContent(String.class);
However the body comes as null. I looked into cxf code & it looks like you have to specify the exact class (Ex : ArrayList) to get the body. My app has multiple such message classes. I wanted to know if there is a method by which I can avoid writing multiple checks for each of my POJO class & do it in a single if.
You could call message.getContent(InputStream.class) and use CXF IOUtils to read into String. Please refer javatips.net/blog/cxf-interceptor-example for more details
try:
XMLStreamReader body = message.getContent(XMLStreamReader.class);
Related
GET http://localhost/foo/api/v1/bars/:id
How to have different JSON responses registered for a GET call. We would like the GET call to return a separate response based on whether a CLI is invoking or the user interface is calling the API by passing a query parameter. But how do we register different serializers dynamically on the response.
You can use a User-Agent request header to identify the application doing the request. There are good tutorials to check how to access the headers in Spring, like this Baeldung one.
I'm facing a particular use case while using Wiremock standalone API.
I would like to be able to reuse a response body generated by stubbing for a another request (stubbed as well) as a context model. The purpose is to store for a generated Id the entire response data, that would allow me to serve it again simply knowing the Id, in a get method particularly (where there is no request body).
Is there a way while defining a stub of response to capture the generated response, in order to store it?
Or if you have other better idea.
Finally I solved the problem by using an okhttp interceptor (which depends on your client solution).
In the interceptor, I store every response data (e.g.: a generated ID) and set them in every next request headers when it matches with part of the the response stored.
adding them to the request headers allows me to access them in a json template file for instance
I need make my method capable of accepting JSON data. Say I need to make a GET REST method call with JSON data.
GET /player/login/ HTTP/1.0
Content-Type: application/json
Request Body
{
"username": ”xyz”,
"password": "234fsf34"
}
I am not getting how to take this JSON data within my REST API method.
#GET
#Path("player/login")
#Produces("application/json)
public responseData loginPlayer(){
}
If you want to use authentication with a GET, the 'proper' way to do it is to use basic access authentication.
http://en.wikipedia.org/wiki/Basic_access_authentication
The client takes the username and password and forms a string of the form user:password. So in your example, that will be:
"xyz:234fsf34"
The client then base64 encodes this string. If the client is also Java, you could use the apache commons Base64 class for encoding/decoding:
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
So you get something like:
"eHl6OjIzNGZzZjM0"
And the client sends this in the GET, but as an HTTP Header (not Request Body):
GET /player/login/ HTTP/1.0
Content-Type: application/json
Authorization: Basic eHl6OjIzNGZzZjM0
The server reads this in the HTTP Header 'Authorization' much like the example code here:
https://cwiki.apache.org/WINK/jax-rs-http-headers.html
Then decodes it with the (Base64 apache commons class).
Then you can respond to the GET with appropriate data.
From the syntax, it appears you are using JAX-RS.
You need to use the #Consumes annotation.
The documentation is for #Consumes is at:
http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html#gipyt
A rather detailed tutorial (which uses Jersey, the reference JAX-RS implementation) is here:
http://blogs.oracle.com/enterprisetechtips/entry/configuring_json_for_restful_web
Eventually, your method should be defined to accept the deserialized java object corresponding to the JSON body received from the client, and you need to set up things so that this deserialization is done before your method gets invoked. Read the second link for how to do it.
On a side note: GET methods typically do not have a 'Request Body'. It is kind of weird to have that. The POST method is meant for posting request bodies. While the HTTP standard does not explicitly disallow GET requests to have a body, POST is the appropriate choice.
How, if possible, do I get the raw XML request/response that is invoked/retrieved by Axis in my application?
I'm using WSDL2Java that is included with Axis to generate the Java stubs.
EDIT:
What I currently have is an app that uses Axis to handle the remote API calls.
One of the requirement is to 'store' all the XML request/response from these calls in the session so that it will be available in the JSP (for debugging purposes). How can I achieve this?
I tried writing a custom handler that extends BasicHandler but in that handler, I still can't get the HttpServletRequest/HttpServletResponse pair from the MessageContext
After a while searching its as simple as this:
//After your _call.invoke(...);
//Request
String request = _call.getMessageContext().getRequestMessage().getSOAPPart().getEnvelope().getBody().toString();
//Response
String response = _call.getMessageContext().getResponseMessage().getSOAPPart().getEnvelope().getBody().toString();
where _call is org.apache.axis.client.Call
Then you can save it in a file where you want...
Why don't you write a server side soap handler, get hold of MessageContext and I believe there is a way to get hold of the payload from there. If you want to pass it to downstream then put it in thread local. See e.g. of handler here
I end up using the solution described in this question
Basically, I use it to get a hold of the HttpServletRequest and from there I set the proper item in the session.
I have a Java WebService setup which consumes an xml file and want to be able to produce either xml or json based on what the client requests. I know that this is possible through reading up on Jersey REST methods but it does not show how to extract this information. I have also looked on google all over but can't seem to find any examples of this.
http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features is the site that I was initially referencing which shows that it is possible, I was just wondering if anyone would be able to help me find out how to actually distinguish the client's request. Is it in the html header? body? And if so what is the proper way to extract it?
This is what my method currently looks like, I do not have any issues with connection, just finding out what the client requests as a return type.
#POST
#Path("getStatisticData")
#Produces ({"application/xml","application/json"})
#Consumes ("application/xml")
public String getStatisticData(#FormParam("xmlCoords") String xmlFile) throws Exception{
Thanks in advance.
You can extract it using the #HeaderParam annotation:
...
public String getStatisticData(#HeaderParam("Accept") String accept,
#FormParam("xmlCoords") String xmlFile) throws Exception {
...
}
The Accept header in the request is used for the client to indicate to the server what methods it supports.
If the client can set HTTP headers, the proper way to do it is to use the Accept header:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
IF this is not possible, the type requested could be passed in as an argument.
Alternatively, expose two different web services: one that returns XML, one that returns JSON. Each web service would call the same code but with a parameter specifying which format to use.