I'm following this tutorial where I'm creating a REST web service in a Spring boot application with Jersey:
I'm getting an error in initialization. I cannot figure out what is it.
when I type in:
http://localhost:8080/rest/books
I get this error:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Apr 20 12:57:24 WAT 2017 There was an unexpected error (type=Internal Server Error, status=500). Validation of the application resource model has failed during application initialization. [[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.first.rest.test.controller.BookController.updateBook(java.lang.String,com.first.rest.test.model.Book) at index 0.; source='ResourceMethod{httpMethod=PUT, consumedTypes=[application/json], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.first.rest.test.controller.BookController, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@642e8d2e]}, definitionMethod=public javax.ws.rs.core.Response com.first.rest.test.controller.BookController.updateBook(java.lang.String,com.first.rest.test.model.Book), parameters=[Parameter [type=class java.lang.String, source=oid, defaultValue=null], Parameter [type=class com.first.rest.test.model.Book, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
And in the console I have this
017-04-20 13:06:44.211 ERROR 1516 --- [nio-8080-exec-1] c.c.C.[.[.[.[.f.r.t.JerseyConfiguration] : Allocate exception for
servlet com.first.rest.test.JerseyConfiguration
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during
application initialization. [[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response
The project structure is:
Classes: Main App class:
@SpringBootApplication
public class FirstRestApplication {
public static void main(String[] args) {
SpringApplication.run(FirstRestApplication.class, args);
}
Jersey Config class:
@Configuration
@ApplicationPath("rest")
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
}
@PostConstruct
public void setUp() {
register(BookController.class);
register(GenericExceptionMapper.class);
}
}
Book Controller class:
@Component
@Path("/books")
public class BookController {
private BookService bookService;
public BookController(BookService bookService) {
this.bookService = bookService;
}
@GET
@Produces("application/json")
public Collection<Book> getAllBooks() {
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();
}
Book model
public class Book {
private String oid;
private String name;
private String author;
private Category category;
public Book() {
}
public Book(String oid, String name, String author, Category category) {
super();
this.oid = oid;
this.name = name;
this.author = author;
this.category = category;
}
public String getOid() {
return oid;
}
//more code
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.first.rest.test</groupId>
<artifactId>firstrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>first rest</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>