In the previous posts, we have created a Spring Boot QuickStart, customized the embedded server and properties and running specific code after spring boot application starts.
Now in this post, we will create Restful webservices with Jersey deployed on Undertow as a Spring Boot Application.
Adding dependencies in pom.xml
We will add spring-boot-starter-parent
as parent of our maven based project. The added benefit of this is version management for spring dependencies.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.0.RELEASE</version> </parent>
Adding spring-boot-starter-jersey dependency
This will add/ configure the jersey related dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency>
Adding spring-boot-starter-undertow dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
These are all the necessary spring-boot-starters we require to create Restful webservices with Jersey.
Creating a Root resource/ Controller class
What are Root resource classes?
Root resource classes are POJOs that are either annotated with @Path or have at least one method annotated with @Path or a request method designator, such as @GET, @PUT, @POST, or @DELETE.
@Component @Path("/books") public class BookController { private BookService bookService; public BookController(BookService bookService) { this.bookService = bookService; } @GET @Produces("application/json") public CollectiongetAllBooks() { return bookService.getAllBooks(); } @GET @Produces("application/json") @Path("/{oid}") public Book getBook(@PathParam("oid") String oid) { return bookService.getBook(oid); } @POST @Produces("application/json") @Consumes("application/json") public Response addBook(Book book) { bookService.addBook(book); return Response.created(URI.create("/" + book.getOid())).build(); } @PUT @Consumes("application/json") @Path("/{oid}") public Response updateBook(@PathParam("oid") String oid, Book book) { bookService.updateBook(oid, book); return Response.noContent().build(); } @DELETE @Path("/{oid}") public Response deleteBook(@PathParam("oid") String oid) { bookService.deleteBook(oid); return Response.ok().build(); } }
We have created a BookController
class and used JAX-RS annotations.
@Path
is used to identify the URI path (relative) that a resource class or class method will serve requests for.@PathParam
is used to bind the value of a URI template parameter or a path segment containing the template parameter to a resource method parameter, resource class field, or resource class bean property. The value is URL decoded unless this is disabled using the @Encoded annotation.@GET
indicates that annotated method handles HTTP GET requests.@POST
indicates that annotated method handles HTTP POST requests.@PUT
indicates that annotated method handles HTTP PUT requests.@DELETE
indicates that annotated method handles HTTP DELETE requests.@Produces
defines a media-type that the resource method can produce.@Consumes
defines a media-type that the resource method can accept.
You might have noticed that we have annotated BookController
with @Component
which is Spring's annotation and register it as bean. We have done so to benefit Spring's DI for injecting BookService
service class.
Creating a JerseyConfiguration class
@Configuration @ApplicationPath("rest") public class JerseyConfiguration extends ResourceConfig { public JerseyConfiguration() { } @PostConstruct public void setUp() { register(BookController.class); register(GenericExceptionMapper.class); } }
We created a JerseyConfiguration
class which extends the ResourceConfig
from package org.glassfish.jersey.server
which configures the web application. In the setUp()
, we registered BookController
and GenericExceptionMapper
.
@ApplicationPath
identifies the application path that serves as the base URI for all the resources.
Registering exception mappers
Could there be a case that some exceptions occurs in the resource methods (Runtime/ Checked). You can write your own custom exception mappers to map Java exceptions to javax.ws.rs.core.Response
.
@Provider public class GenericExceptionMapper implements ExceptionMapper{ @Override public Response toResponse(Throwable exception) { return Response.serverError().entity(exception.getMessage()).build(); } }
We have created a generic exception handler by catching Throwable
. Ideally, you should write finer-grained exception mapper.
What is @Provider
annotation?
It marks an implementation of an extension interface that should be discoverable by JAX-RS runtime during a provider scanning phase.
We have also created service BookService
, model Book
also. You can grab the full code from Githib.
Running the application
You can use maven to directly run it with mvn spring-boot:run
command or can create a jar and run it.
Testing the rest endpoints
I have used PostMan extension available in chrome brower to test rest services. You can use any package/ API/ software to test it.
This is how we create Restful web-services with Jersey in conjuction with Spring Boot. I hope you find this post informative and helpful to create your first but not last Restful web-service.
How do i test?
ReplyDeleteIm getting: 2017-02-24 12:08:44.733 WARN 6168 --- [ XNIO-3 task-1] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
for this:
{
"timestamp": 1487948924757,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'PUT' not supported",
"path": "/books"
}
You need to pass the oid as path param of book along with full payload in the body.
Deleteurl should be like something
DeletePOST localhost:port_number/books/{book_oid}
then in the body the whole book object as json
and also one header Content-Type with value application/json
even downloading your project and running i get
DeleteHTTP method PUT is not supported by this URL
it seems that something is wrong or missing
URL: http://localhost:8080/books/{oid} (changed the port to 8080)
body {
"oid" : "1",
"name" : "some singer",
"author" : "some singer",
"category" : "some singer"
}
header Content-Type application/json
If you are following the project, then url need modification
DeleteURL: http://localhost:8080/rest/books/1 (changed the port to 8080)
body {
"oid" : "1",
"name" : "some singer",
"author" : "some singer",
"category" : "some singer"
}
here 1 is the book's oid to update. It is a path param.
I also added "rest" in the url because if you see JerseyConfiguration class, I have used the @ApplicationPath("rest") which says we need to add "rest" in the url.
ok, that worked, tks!
DeleteNice tutorial. I was wondering it will be great help if you can write some junit test cases for the same project. I'm facing lot of issues while writing the test cases. Thank You!
ReplyDelete