I have a Spring MVC REST service that uses XStream to convert the messages to and from XML.
Is there any way I can print the xml (ie the body) from the request and response to a normal log4j logger?
Inside the controller won't work cause by then XStream has alread unmarshalled the request and not yet marshalled the response.
A filter in the servlet isn't very nice either as it will consume the body by reading it.
And thats where I run out if ideas. So, SO? Any takers? :)
You can use an interceptor to log your request and response content coming to your specific rest uri's - More details here
Related
I've read https://www.playframework.com/documentation/2.5.x/ScalaHttpFilters#more-powerful-filters, but I still don't understand how to access the request body inside the filter chain. I'm trying to make an accumulator but I'm not sure how to access the nextFilter in the apply method of EssentialAction. If anyone knows how to actually access the request body inside the filter chain let me know! I'm working in java
In order to parse the body of the request, you can use Action composition in your filters. You must actually stream / parse the body in the filter manually, and then let the framework parse it again as it passes to your controller.
Here is a good article on how this can be achieved.
https://www.javacodegeeks.com/2013/02/understanding-the-play-filter-api.html
One of my application's methods uses javax.ws.rs.Client to POST a request with an XML payload to an external REST service and receive back an XML response. I then convert this response to JSON and return it to the method caller. How do I write a unit test that mocks out the external REST service, so I can focus on sending an XML payload, receiving back an XML response which is converted to JSON, so I can look for a specific property in the JSON to pass the test? I need to use Mockito testing framework.
I imagine the test method would need access to sample XML request and XML response strings (w/ hard coded values). If so, where would I store these strings which are a bit lengthy, particularly the response?
I have a spring application, restful/mvc with a basic spring controller. Also, internally jackson is used to map the json data into beans. When the client sends bad data say, an extra field is added in the JSON body there the controller returns with a 400 bad request error to the client. This is for post requests. Is there a way to handle the 400 error and capture the error and log it.
I am trying to implement the example shown here.
The example seems to be for a jersey setup, which I am not using or familiar. How hard would it be to convert this to a standard java servlet project(idk how to name this)
What steps should I take. It seems most of the # annotations need to be changed to servlets.
This also seems very differnt from the standard appengine upload setup which all takes place in one servlet.
This would be a lot of work to rework the code to standard servlet and remove jersey. Jersey takes away so much boilerplate code. For example the JSON conversion is done by jersey, which otherwise would have to be custom implemented.
And you can for sure deploy more than one servlet to gae, in which way should this be standard?
Just look at the first method:
#GET
#Path("/url")
public Response getCallbackUrl() {
String url = blobstoreService.createUploadUrl("/rest/file");
return Response.ok(new FileUrl(url)).build();
}
When using only standard servlet you would need to do:
Servlet Definition and Mapping in web.xml to /url
Implement a HttpServlet, override doGet() method
Send Response Code 200 OK
Set appropriate HTTP Response Headers
Convert Response to JSON and write it to response
I am creating a simple RESTful web service with simple types successfully. Now I want to pass an object as argument for web service and get the object as response. My scenario is, Parse the XML message as object by using Jaxb and send the object as request for web service. After that in server side it process the requested object and generates the response xml file and send back it as object.
In URL path i give
"http://localhost:8080/SampleWS/rest/checkXML/username=visolve&password=visolve"
for simple type. But in object I don't know how to give the object reference in URL. Please help me how to solve my problem..
Regards
Bathakarai
Just define a very good-looking domain object. JAXB and JAX-RS will do the rest.
JAXB.
#XmlRootElement
class Regards {
#XmlElement
private long sincerely;
}
JAX-RS.
#Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
#POST
#Path("/sincerely")
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response sincerely(final Regards regards) {
regards.setSincerely(System.currentTimeMillis());
return Response.ok(regards).build();
}
Though you could certainly include the entire XML content in your URL, I would probably shy away from it.
Think of it this way: if you encode the XML in the URL you're potentially adding more work on both ends. Now the server and client will both need to know how to build the URL properly, and check to make sure everything lines up correctly. What if, in the future, you need to offer a JSON or YAML view of the same content? Now your URL might need to include the content-type as well. What about character-encoding?
All this to say, HTTP provides a terrific transport mechanism which already addresses these concerns. Include the XML as the entity body of the HTTP message, and use the HTTP header to identify what content-type you're sending, character-encoding, etc. This will work both ways (the server and client both can send the XML back/forth), and makes better use of HTTP.
Here's a related link which might help with some of the details. And another.
On a side note, please, please, please tell me you don't plan on sending user-credentials in plain text across an unencrypted link.