[JAX-RS] Use @Provider to leverage JAX-RS provider auto-discovery feature.

There are at least 2 ways to register a JAX-RS resource such as a set of web methods or the filters. 1. The first and the verbose way is to override the getClasses method in a subclass of Application. Like this:

The drawback with this approach is that you have to explicitly add all…

[JAX-WS] Simple Dispatch Client Example

Here is an example of a Dispatch type Java SE WebService client. 1. A sample Web Service (Running as a Dynamic Web Application in Glassfish):

  2. Here is the client (A simple Java SE application). Make sure to start the previous application first:

This will generate a SOAP request like:

And…

Low Level SOAP manipulation and JAX-WS invokation (SAAJ)

1. The Client Code

2. The server (a very simple Dynamic Web App in eclipse running on Glassfish):

3. Invokation results: 3.1. Request

3.2. Response

[JAX-RS] Implementing custom MessageBodyWriter for serialization

Say you want to serialize your object in a custom format. For XML you can use JAXB, for JSON you can use Jackson. But in case you have your own specific format, you need to implement your own MessageBodyWriter. 1. Maven web application – pom.xml (here jersey 1.19, feel free to upgrade):

  2….

[JAX-RS] ExceptionMapper example (Maven + Jersey + Glassfish)

Create a new Maven project (Web app archetype) in eclipse and add the dependencies on jersey. Also you will need Glassfish server. 1. pom.xml

2. Main Application

3. An example service

4. The Exception Mapping class. Here we’re mapping the IllegalArgumentException to a BAD_REQUEST

 

[JAX-RS] Using CDI for dependency injection in JAX-RS

1. The resource class

  2. MyService interface

3. MyServiceImpl

4. web.xml (under /WEB-INF/)

5. beans.xml (under /WEB-INF/)

6. Deploy on glassfish! and access it: http://localhost:8080/TestCDI/rest/cdiexample (given that the project name is TestCDI).