In our JAX-RS webservice using Jersey tutorial, we have created a restful webservice that contains two methods. Now, we will try to create a client and call the
ping()
method using Pure JAX-RS API without using Jersey and also using Jersey API.Our test case, HelloAngryWorldTest.java, has two methods, one using JAX-RS API and other using Jersey:
1. HelloAngryWorldTest.java :
loading...
There are some inter dependencies between the jar files being used for Pure JAX-RS client API and Jersey Client API. Hence, let us try to run each client separately. That is, to run a method, we need to comment out the other and viceversa.
testPingUsingPureJaxRS()
method uses pure JAX-RS classes for creating the response. Using pure JAX-RS API, the response creation will be as follows :Client -> WebTarget -> Invocation.Builder -> Response
testPingUsingJersey()
uses Jersey API, the response creation is as below :Client -> WebResource -> ClientResponse
There are number of ways you can test the Restful webservice. Before running any of the below, make sure the tomcat server is started and the webservice is running using,
mvn clean package -DskipTests=true tomcat7:run
or mvn clean package -DskipTests=true jetty:run
1. Using maven command :
i. Specify the service.url at command line :
mvn -Dservice.url=http://localhost:<port-number>/jaxrs-service test
This command uses a command line argument to specify the service.url. The service.url is used in the test case.
ii. Specify the service.url in pom.xml
loading...
Add the above configuration and use the below command to test the webservice,
mvn test
iii. Hard-code the service.url in HelloWorldTest.java and use the below command to run the tests.
mvn test
Output:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.learninjava.HelloAngryWorldTest
Response from web service is : Tweet
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 43.997 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.learninjava.HelloAngryWorldTest
Response from web service is : Tweet
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 43.997 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
Now try to run the
testPingUsingJersey()
method by commenting the JAX-RS related configuration and enabling the Jersey API configuration. You should see the same output as above. The idea is that you can use either way to create a Jersey Client.2. Using Poster plugin :
Poster is a plugin for firefox browser. This is a very useful and handy tool to test your restful webservices quickly.
i. To test the ping method of HelloAngryWorld, enter the following in Poster UI :
URL: http://localhost:<port-number>/jaxrs-service/helloAngryWorld/echo/tweet
Content Type: application/text
and click on GET button as ping is a GET request type. The response is displayed in the Response window.
ii. To test the
modifyJson()
method of HelloAngryWorld, enter the following in Poster UI :URL: http://localhost:<port-number>/jaxrs-service/helloAngryWorld/jsonBean
Content Type: application/json
Payload(Text Box at bottom): { "val1": "Birds", "val2": "Eggs" }
and click on POST button as modifyJson is a POST request type. The response is displayed in the Response window.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by or or below or on the articles on social media.