Often times, you want to make RESTful API consumers capable of filtering data what they need from a GET API, rather than forcing them to consume all the response data unnecessary. There are two ways to achieve this filtering : server side response filtering and client side response filtering. Server side response filtering, though useful, […]
Jersey – How to set Cookie in REST API Response
In this example, we will learn to set cookies into HTTP responses sent by Jersey REST APIs. This example makes use of javax.ws.rs.core.Response for setting cookies into REST responses sent to REST clients. Set Cookie Syntax To set a cookie in REST API response, get the Response reference and use it’s cookie() method. Rest API […]
Jersey Client – Set Cookie Example
In this example, we will learn to set cookies into HTTP requests invoked by Jersey client. This example makes use of Invocation.Builder for setting cookies into outgoing REST calls. Set Cookie Example To set a cookie in REST API request, first get reference of Invocation.Builder from webTarget.request() method, and then use it’s methods. Rest API […]
Jersey Logging Request and Response Entities using Filter
By default, Jersey uses JUL for logging – and does not print request/response entity bodies in logs. To print entity content, you must create your own LoggingFiler, and register it in place of default org.glassfish.jersey.filter.LoggingFilter. In this example, I am creating one such basic CustomLoggingFilter which extends org.glassfish.jersey.filter.LoggingFilter and implements ContainerRequestFilter and ContainerResponseFilter interfaces so […]
Jersey REST Client Authentication Example
Learn to use Jersey REST client authentication using HttpAuthenticationFeature, which can be used to access REST APIs behind authentication security.
Jersey REST API Security Example
In this Jersey rest security example, we will learn to secure Jersey REST APIs with basic authentication. This will make mandatory every user to provide username/password to authenticate into portal. Also, user must have certain level of role as well. I have extended this example from my other example created for RESTEasy API security and […]
Jersey + Google Gson Example
This tutorial explains how to use google Gson with Jersey 2.x. There are some other java libraries also capable of doing this conversion e.g. MOXy, Jackson or JSONP; but Gson stands among very few which do not require any pre-annotated java classes OR sourcecode of java classes in any way. Table of Contents Maven dependencies/changes […]
Jersey + JSONP Example
This tutorial explains how to use JSONP JSON provider with Jersey 2.x. JSONP is also auto-discoverable just like what we discussed in Jersey MOXy example. Table of Contents JSONP maven dependencies/changes REST API code Model bean changes Manually adding JsonProcessingFeature JSONP maven dependencies/changes JSONP media module is one of the modules in Jersey 2.x where […]
Jersey + MOXy JSON Example
This tutorial explains how to use MOXy JSON feature with Jersey 2.x. MOXy is the default JSON-Binding Provider in Jersey 2.x. Though I still personally prefer Jackson over MOXy for performance reasons. Table of Contents MOXy maven dependencies/changes REST API code Model bean changes Manually adding MoxyJsonFeature Customize behavior using MoxyJsonConfig Demo MOXy maven dependencies/changes […]
Jersey-quickstart-archetype Hello World Example
In this example, we will create a jersey RESTful API web application using jersey-quickstart-webapp maven archetype in eclipse. Table of Contents Install remote archetypes in eclipse Create new maven project using jersey-quickstart-webapp Generated Files Run The Application Install remote archetypes in eclipse The very first step before creating actual maven quick-start application, is to install […]
Jersey – Case-insensitive @Path URLs
By default in JAX-RS, ALL URLs specified in @Path annotations are CASE-SENSITIVE. In this tutorial, I am giving a simple example of CASE-INSENSITIVE URLs which cane be used inside @Path annotation on any Jersey JAX-RS REST API. How to make URLs case in-sensitive To make URLs case-insensitive, change the @Path URLs like below: Change above […]
Jersey Client Example – Jersey 2 Client API
Jersey 2 client API finds inspiration in the proprietary Jersey 1.x Client API. In this Jersey client example, we will learn to build client API and invode different REST methods and consule the API results.
Jersey exception handling – Jersey ExceptionMapper Example
In Jersey ExceptionMapper example, we will learn to handle custom exceptions using ExceptionMapper interface while developing Jersey RESTful web services. For demo purpose, I am modifying the sourcecode written for jersey download file example. Table Of Contents 1. Jersey custom exception with ExceptionMapper 2. How to throw exception from REST API 3. Demo 1. Jersey […]
Jersey – Ajax Multi-File Upload Example
Learn to use Ajax with JAX-RS webservices (Jersey used in example) to upload multiple files with single button click. Also look at form based file upload example and file download example as well. Table of Contents Jersey maven multipart dependency Add MultiPartFeature Write Upload REST API HTML/Ajax code Demo of Multiple files upload Jersey maven […]
Jersey file upload example – jersey 2 MultiPartFeature
In this Jersey file upload example, learn to upload binary files (e.g. PDF files in this example) using Jersey’s multipart support for file upload.
Jersey file download example – StreamingOutput
In this Jersey file download example, we will learn to write a Jersey rest api which will be able to stream or download file (e.g. PDF/Excel/Text files) to requesting client. I will be using javax.ws.rs.core.StreamingOutput class for building this JAX-RS API. Table of Contents 1. REST API to stream file with StreamingOutput 2. Jersey file […]
[Solved] java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
You can face this exception if you are setting up jersey 2 project first time in eclipse using maven plugin. Message is simple enough to identify the root cause the Jersey libraries are not in classpath. Exception will look like this. Solution – Add Jersey Library in Deployment Assembly Open your project’s deployment assembly configuration. […]
Jersey 2 hello world example – Jersey 2 tutorial
I have published lots of tutorials for developing REST apis using RESTEasy. In this Jersey 2 tutorial, I will go through configuration steps in detail for setting up a Jersey 2 example web application project. Table of Contents 1. What changed from Jersey 1.x to Jersey 2.x 2. Jersey 2 maven dependencies 3. web.xml Changes […]
Jersey Hello World Example
I have written a number of posts on JAX-RS RESTEasy concepts and how to. Now I have started exploring Jersey which is another popular framework for making RESTFul applications. To start with, I am writing my hello world application in this post, which I will modify in next posts to show demos of other features […]
SOLVED: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
If you have start working on Jersey then you might face this issue while configuring it. This is the result of insufficient project dependencies on runtime. This issue is likely to be faced in tomcat server. The error log will look like this. To solve this issue, correct the project dependencies in pom.xml like below. […]