请帮忙。我正在开发一个使用Spring JPA,Hateoas和Spring Data Rest MVC的Spring Boot应用程序。该应用程序可以分为RESTFul API和Web App两个主要部分。 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,只是为了表明我正在使用@RestController作为RESTful API而@Controller用于Web App。
如何让Spring为JSP页面提供服务?我不想使用xml。我想在一个项目中同时拥有RESTful API和Web App。
提前谢谢。
答案 0 :(得分:0)
这就是我最终的目标。
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偶然发现了这一点。