How to output the generated request and response from Groovy RestClient? - rest

i am currently using the RestClient and cannot seem to figure out how to output the request xml and response xml for debugging and informational purpose...
I tried the solution mentioned here:
http://agileice.blogspot.com/2009/09/pretty-printing-xml-results-returned.html
But that fails to work, any other suggestions?

The accepted answer (turn on wire logging using log4j) is basically correct, but I've had a fair bit of trouble turning on wire logging for HTTP builder in my Groovy script. For some reason, dropping a log4j.xml file in my $GROOVY_HOME/conf directory isn't working. Ultimately, I had to just add the appropriate logging options to the groovy command when I was running it.
groovy
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
-Dorg.apache.commons.logging.simplelog.showdatetime=true
-Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG
myscript.groovy

Since it depends on HTTPClient, you could try enabling header and wire logging for your script.
http://blog.techstacks.com/2009/12/configuring-wire-logging-in-groovy-httpbuilder.html
http://hc.apache.org/httpcomponents-client-ga/logging.html

If you're using spring-boot you can set logging.level in your application properties file and use an slf4j back-end.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

Related

Necessity for declaring RestEasy dependencies although bundled with WildFly?

According to the RESTEasy modules in WildFly documentation:
In WildFly, RESTEasy and the JAX-RS API are automatically loaded into
your deployment's classpath if and only if you are deploying a JAX-RS
application (as determined by the presence of JAX-RS annotations).
However I don't really understand this paragraph. What does it exactly mean? As an exmaple, let's say I want to use ResteasyClient in a class. My IDE tells me that I must add this dependency in the corresponding pom.xml. But then how does that go with the above quote?
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
</dependency>
My pom.xml already includes this:
<dependency>
<groupId>org.wildfly.bom</groupId>
<artifactId>wildfly-jakartaee8-with-tools</artifactId>
<version>20.0.1.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
When looking at this BOM it looks as if the resteasy-client is already included?
My IDE tells me that I must add this dependency in the corresponding pom.xml
Yes, you must declare this dependency in your pom.xml if you use the API of it, but you only need provided-scope, because as the documentation said, it is already included in your deployment's classpath. If you use only the standard api defined in wildfly-jakartaee8, you do not need this dependency.

GWT 2.8 websocket support

Does jetty server in gwt 2.8 support websocket now? As I know it did not support before. If there is a positive answer, then how to make it work? Stripping out jetty-8 and replaceing it with jetty-9 is not a good idea I think.
then how to make it work?
I want to elaborate a bit on this after the GWT 2.8.0 release. The only thing required for using javax.websocket is the knowledge of the Jetty version packaged with GWT and the following set of Maven dependencies (see also the Jetty WebSocket examples on GitHub):
<project>
<properties>
<sdm.jetty.version>9.2.14.v20151106</sdm.jetty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<version>${sdm.jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<version>${sdm.jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</depencies>
</project>
Make doubly sure that the scope is provided - for the former two this will mean they are not packaged into the final app - you will be requiring those only when running the SuperDev-Mode (SDM). Ifjava.websocket-apiwas on your classpath probably the annotation-based configuration will not work at all (at least in embedded Tomcat and Jetty) due to the annotations being picked up by the wrong class loader (see also related question WebSocket 404 error for more info on this topic).
GWT 2.8 has switched to Jetty 9.2, and now supports Servlets 3.1 servlets container initializers, which I think are being used to setup WebSockets.
I haven't tried it but I suppose that you can now have WebSockets in DevMode, provided you add the required dependencies to the classpath.
You can also simply use a separate server rather than the one embedded into DevMode.

unable to perform multipart form upload using rest-assured

I am trying to upload file using multipart form upload using the following rest-assured method.
given().filter(new RequestLoggingFilter(captor)).when().multiPart("metadata", new File("S:\\testdata.prop")).multiPart("file",aFileStream).post("/uploadFile").then().statusCode(200);
This throws an error that
com.fasterxml.jackson.databind.Module: Provider
com.fasterxml.jackson.datatype.joda.JodaModule could not be
instantiated.
jackson library is added to classpath.the testdata.Prop file is a properties file
You need to add jackson-databind to the classpath. If you're using Maven you can add it like this:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
Also if you're using JodaTime you may need to add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.6.2</version>
</dependency>
REST Assured automatically tries to register all jackson modules in classpath.

SOAP web services NPE on redeployment Tomcat 6

I'm using Tomcat 6 and I have a web application that uses a SOAP web service. I generated the client classes to use using cxf-codegen-plugin in maven, my dependencies are :
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.6.1</version>
</dependency>
Everything works until I redeploy my war on tomcat (or even doing a simple touch on it: no code modification at all). Using the same url to access my page that makes the SOAP service call I have this weird NPE:
java.lang.NullPointerException
at com.ctc.wstx.util.SymbolTable.findSymbol(SymbolTable.java:385)
at com.ctc.wstx.sr.StreamScanner.parseLocalName(StreamScanner.java:1831)
at com.ctc.wstx.sr.BasicStreamReader.handleNsAttrs(BasicStreamReader.java:2997)
at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2941)
at com.ctc.wstx.sr.BasicStreamReader.handleRootElem(BasicStreamReader.java:2078)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2058)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1117)
at com.sun.xml.internal.ws.util.xml.XMLStreamReaderFilter.next(XMLStreamReaderFilter.java:81)
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.next(XMLStreamReaderUtil.java:78)
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.nextContent(XMLStreamReaderUtil.java:99)
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.nextElementContent(XMLStreamReaderUtil.java:89)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.hasWSDLDefinitions(RuntimeWSDLParser.java:209)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:119)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.<init>(Service.java:56)
at some.package.flightinfo.model.flightstatsv2.soap.AirportsV1SoapService.<init>(AirportsV1SoapService.java:48)
at some.package.flightinfo.adapter.FlightStatsV2ContentAdapter.getAirportsByGPSCoordinate(FlightStatsV2ContentAdapter.java:107)
The only way I can get my web application working again is to restart tomcat which is no option at all.
I am totally clueless on what's going on, has anyone ever experienced this problem before?
Cheers.
Well, you are missing the cxf-rt-frontend-jaxws dependency. Thus, you are ending up using the JAX-WS reference impl built into the JDK, not CXF. If you add the CXF dependencies, does that fix it? Could be a bug in the RI or something.

Jersey: A message body writer for Java Class and MIME mediatype application/json was not found

after trying to figure out what's my problem I finally decided to ask you how to solve my problem. I've seen different people with the same problem and I tried all the things they were adviced to do but nothing helped with my issue. So basically I'm having a RESTful Service which I build using Jersey. For my client I would like to return an object in JSON Format. I read through different tutorials and decided that it makes sense to use jersey-json-1.8 library. I added everything to my project as usual and tried to run it but each time I'm calling the service (via get request atm.) I'm getting HTTP Error Code 500 (internal server error) and my server responds that no message body writer could be found.
If I'm returning XML it works just fine and everything is great. I also tried copying jersey-json-1.8.jar to my Tomcat lib folder because I had to do this with the mysql lib I'm using but it didn't help either.
I would be really glad if you could help me out to get this stuff working!
If you need any more information just leave a comment and I'll provide it as quickly as humanly possibly :)
My project setup is:
3 Different packages
1. My RESTfulServices
2. My Java Work where i handle SQL connections etc.
3. A package where I store all my models that I need to work with and that I want to return in JSON Format (in my example a route for a testdrive)
A Tomcat Webserver
IDE: Eclipse
I'm using Maven
No matter what or how I'm trying to return the Object it just won't work and I'm constantly getting the error message :
Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.mykong.models.Teststrecke, and Java type class com.mykong.models.Teststrecke, and MIME media type application/json was not found
EDIT:
Here's my JSON Service Method
#Path("/hellojson")
public class JSONService {
#GET
#Produces(MediaType.APPLICATION_JSON)
public ArrayList<Route> getJSONMsg()
{
Route ts = new Route();
ts.setId(1);
ts.setName("HelloWorld");
Route ts2 = new Route();
ts2.setId(2);
ts2.setName("HelloWorld");
ArrayList<Route> availRoutes = new ArrayList<Route>();
availRoutes.add(ts);
availRoutes.add(ts2);
return availRoutes;
}
}
Try adding Genson library to your classpath http://owlike.github.io/genson/.
It is a json<>java streaming and databinding api. It integrates well with jersey, to get you running you need 0 configuration. Jersey detects that the library is in your classpath and enables json mapping by delegating to Genson.
Include this dependencies in your POM.xml and run Maven -> Update
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
Adding below dependency solved my issue. As per #eugen I tried adding Genson dependency which helped get rid of the exception, however my server was throwing 400 bad request for some reason. It seems like Genson was not able to stream the json request properly. But below dependency worked fine. Hope it helps others as this problem was driving me nuts!
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
</dependency>
Moving jersey-json dependency to the top of the pom.xml solved this problem for me.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<!-- other dependencies -->
</dependencies>
Check under Maven Dependencies in the Package Explorer, if you don't see that or jersey-bundle-1.8.jar, then you probably have specified just jersey-server. Change the dependency in pom.xml to jersey-bundle and it should work.
Jersey 2.4: I faced this problem too and posting this incase it helps someone. I picked this up from the Jersey API Specification for JSON Integration : (https://jersey.java.net/documentation/latest/user-guide.html#json)
I used Jettison to integration with Jersey with JSON. But even after adding all the libraries I got the error: A message body writer for Java Class and MIME mediatype application/json was not found
Its all about adding the right libraries and also registering features> I added one extra library in addition to all the others that Jersey provides: jersey-media-json-jettison-2.4.jar. And added the below code to register Jettison Feature.
public class MyApplication extends ResourceConfig{
public MyApplication() {
register(JettisonFeature.class);
packages("org.XXX.XXX.XXX");
}
This question was asked a while ago, but for those reading, note that jersey servlet container has it's own POJO mapper.
Enable it by adding the following to your web.xml
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
I did notice some discrepancies in how jersey and genson map objects. For example, if a class has a field of type A which is set to an instance of B, which extends A, genson will not properly serialize B, while jersey does.
Remove #Produces(MediaType.APPLICATION_JSON); and use JSONLibraries.zip file to convert your data to and from JSON. It is working perfectly.The problem with above code is that it is using maven and and jackson combination. So APPLICATION_JSON cannot be found when you are using only jersey.
I worked with to/from JSON conversion stuff, It gave me same error, I tried with this solution and it was working properly.
Code:
#Path("/hellojson")
public class JSONService {
#GET
public ArrayList<Route> getJSONMsg()
{
Route ts = new Route();
ts.setId(1);
ts.setName("HelloWorld");
Route ts2 = new Route();
ts2.setId(2);
ts2.setName("HelloWorld");
ArrayList<Route> availRoutes = new ArrayList<Route>();
availRoutes.add(ts);
availRoutes.add(ts2);
return JSONObject.fromObject(availRoutes.toString()));
}
}
The above issue will be solved by using the jackson library instead of the genson.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
As in my case genson was giving an empty json in the response but will be fine if we are converting the json to string.But will create an undesirable output because in the response you will get a string not a json.So better to go with the jackson library which will do it very easily.
Note: Jackson will convert your response to the json only if the class is implementing the Serializable Interface.So if you are sending JSONObject, then better to send the List of JSONObject.