使用spring boot和jersey的REST webservice。错误:在应用程序初始化期间验证应用程序资源模型失败

时间:2017-04-20 12:11:16

标签: java web-services rest maven jersey

我跟随this tutorial关注我在使用Jersey的Spring启动应用程序中创建REST Web服务:

我在初始化时遇到错误。我无法弄清楚它是什么。

当我输入时:

http://localhost:8080/rest/books

我收到此错误:

  

这个应用程序没有/ error的显式映射,所以你看到了   这是一个后备。

     

Thu Apr 20 12:57:24 WAT 2017出现意外错误   (type = Internal Server Error,status = 500)。验证   应用程序资源模型在应用程序中失败   初始化。 [[致命]没有找到参数的注入源   键入public javax.ws.rs.core.Response   com.first.rest.test.controller.BookController.updateBook(java.lang.String中,com.first.rest.test.model.Book)   在索引0。源=' {ResourceMethod =列举HTTPMethod PUT,   consumeTypes = [application / json],generatedTypes = [],suspended = false,   suspendTimeout = 0,suspendTimeoutUnit = MILLISECONDS,   可调用= Invocable的{处理机= ClassBasedMethodHandler {handlerClass =类   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],参数[type = class   com.first.rest.test.model.Book,source = null,defaultValue = null]],   responseType = class javax.ws.rs.core.Response},nameBindings = []}']

在控制台中我有这个

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
     

应用程序初始化。       [[FATAL]找不到类型为public javax.ws.rs.core.Response的参数的注入源

项目结构是:

project structure Spring boot application

类:  主要App类:

   @SpringBootApplication
public class FirstRestApplication {
    public static void main(String[] args) {
        SpringApplication.run(FirstRestApplication.class, args);
    }

Jersey Config类:

@Configuration
@ApplicationPath("rest")
public class JerseyConfiguration extends ResourceConfig {
    public JerseyConfiguration() {

    }

    @PostConstruct
    public void setUp() {
        register(BookController.class);
        register(GenericExceptionMapper.class);
    }
}

Book Controller类:

 @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();
    }

预订模型

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>

0 个答案:

没有答案