在 Spring 引导应用程序中未找到具有 URI [/WEB-INF/jsp/index.jsp] 的 HTTP 请求的映射

No mapping found for HTTP request with URI [/WEB-INF/jsp/index.jsp] in Spring Boot Application

请帮忙。我正在开发一个 Spring 启动应用程序,它使用 Spring JPA、Hateoas 和 Spring Data Rest MVC。该应用程序可以分为 RESTFul API 和 Web 应用程序两个主要部分。 Restful API 完美运行。现在唯一的问题是让我的 JSP 页面正常工作。我在尝试 http://localhost:9003/home.

时收到以下日志消息
2015-04-06 21:09:02.016 WARN 6702 --- [nio-9003-exec-3] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'dispatcherServletRegistration'

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootApplication
@EnableJpaRepositories
@EnableWebMvc

public class Application {

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

    @Bean
    public ViewResolver viewResolver() {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
       viewResolver.setPrefix("/WEB-INF/jsp/");
       viewResolver.setSuffix(".jsp");
       return viewResolver;
   }

}

WebController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WebController {

    @RequestMapping(value = "/home", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView index() {
        return new ModelAndView("index");
    }

}

DVDController.java

import com.stalinkay.rentadvd.domain.DVD;
import com.stalinkay.rentadvd.service.DVDService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dvd")
public class DVDController {

    private final DVDService dvdService;

    @Autowired
    public DVDController(DVDService dvdService) {
        this.dvdService = dvdService;
    }

    /**
     * Create DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    HttpHeaders create(@RequestBody DVD requestDVD) {
        return dvdService.create(requestDVD);
    }

    /**
     * Retrieve DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    Resource<DVD> retrieve(@PathVariable Long dvdid) {
        return dvdService.retrieve(dvdid);
    }

    /**
     * Retrieve All DVDs
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    List<Resource<DVD>> retrieveAll() {
        return dvdService.retrieveAll();
    }

    /**
     * Update DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    HttpHeaders update(@RequestBody DVD requestDVD) {
        return dvdService.update(requestDVD);
    }

    /**
     * Delete DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    void delete(@PathVariable Long dvdid) {
        dvdService.delete(dvdid);
    }
}

我包含我的 DVDController.java 只是为了表明我正在为 RESTful API 使用 @RestController 和为 Web 应用程序使用 @Controller。

我怎样才能让 Spring 为我的 JSP 页面提供服务?我不想使用 xml。我想在 1 个项目中同时拥有 RESTful API 和 Web 应用程序。

提前致谢。

这就是我的结局。

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}

已将 application.properties 更新为:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

我有 @EnableWebMvc,这也导致了视图解析问题。

我在查看 Spring's spring-boot-sample-web-jsp 时偶然发现了这个。