Tutorial by Examples

First of all for a JAX-RS application must be set a base URI from which all the resources will be available. For that purpose the javax.ws.rs.core.Application class must be extended and annotated with the javax.ws.rs.ApplicationPath annotation. The annotation accepts a string argument which defines...
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello") public class HelloWorldResource { public static final String MESSAGE = "Hello World!"; @GET @Produces("text/plain") public String getHello() { ...
import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; @Path("hello") public class HelloWorldResource { private String message = "Hello StackOverflow!"; @GET @Produces(&...
import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @Path("hello") public class HelloWorldResource { @POST @Path("/receiveParams") public Response receiveHello(@FormParam("name") String ...
@Provider public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> { @Override public Response toResponse(IllegalArgumentException exception) { return Response.serverError().entity("Invalid input: " + exception.getMessage(...
In order to get information about the URI the user agent used to access your resource, you can use the @Context parameter annotation with a UriInfo parameter. The UriInfo object has a few methods that can be used to get different parts of the URI. //server is running on https://localhost:8080, // ...
Sometimes for organizational or other reasons it makes sense to have your top level resource return a sub-resource that would look like this. (Your sub-resource does not need to be an inner class) import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("items&q...
This is an example of how to implement custom parameter converters for JAX-RS endpoints. The example shows two classes from Java 8's java.time library. @Provider public class ParamConverters implements ParamConverterProvider { @Override public <T> ParamConverter<T> getConverter...
Name binding is a concept that allows to say to a JAX-RS runtime that a specific filter or interceptor will be executed only for a specific resource method. When a filter or an interceptor is limited only to a specific resource method we say that it is name-bound. Filters and interceptors that do no...

Page 1 of 1