Spring 此应用程序没有/错误的显式映射
我用maven做教程Spring 此应用程序没有/错误的显式映射,spring,spring-mvc,file-upload,upload,Spring,Spring Mvc,File Upload,Upload,我用maven做教程 我使用的所有代码都被复制了 应用程序可以运行,但我发现错误: 白标签错误页此应用程序没有/Error的显式映射,因此您将其视为回退。 2015年6月30日星期二17:24:02 CST出现意外错误(类型=未找到,状态=404)。 没有可用的消息 如何修复它?您可以通过在应用程序中添加错误控制器来解决此问题。您可以让错误控制器返回所需的视图 我的应用程序中的错误控制器如下所示: import org.springframework.boot.autoconfigure.web
我使用的所有代码都被复制了 应用程序可以运行,但我发现错误: 白标签错误页此应用程序没有/Error的显式映射,因此您将其视为回退。 2015年6月30日星期二17:24:02 CST出现意外错误(类型=未找到,状态=404)。 没有可用的消息
如何修复它?您可以通过在应用程序中添加
错误控制器来解决此问题。您可以让错误控制器返回所需的视图
我的应用程序中的错误控制器如下所示:
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Basic Controller which is called for unhandled errors
*/
@Controller
public class AppErrorController implements ErrorController{
/**
* Error Attributes in the Application
*/
private ErrorAttributes errorAttributes;
private final static String ERROR_PATH = "/error";
/**
* Controller for the Error Controller
* @param errorAttributes
*/
public AppErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
/**
* Supports the HTML Error View
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH, produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request) {
return new ModelAndView("/errors/error", getErrorAttributes(request, false));
}
/**
* Supports other formats like JSON, XML
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Returns the path of the error page.
*
* @return the error path
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
}
}
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
您可以通过实现来选择覆盖默认的ErrorAttributes
。但是在大多数情况下,这个模板应该足够了。本教程希望您在类路径中有ThymleLab模板引擎。我也遇到了同样的问题,最终解决了这个问题。我将与教程作者联系,以包含这些信息
如果您已经学习了本教程,最简单的方法是将依赖项添加到项目根文件夹中的pom.xml中。下次运行应用程序时,Spring将检测Thymeleaf并使用uploadform模板
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
.问题是您导航到的是localhost:8080/而不是指南中规定的localhost:8080/上载。Spring Boot有一个默认错误页面,当您导航到未定义的路由时使用,以避免泄露特定于服务器的详细信息(这可能被视为安全风险)
您可以选择:访问正确的页面、添加自己的登录页或
为了简化这种特殊情况,我更新了指南,使其使用/而不是/upload。确保主类位于根包中,高于其他类
当您运行Spring引导应用程序(即用@SpringBootApplication注释的类)时,Spring将只扫描主类包下面的类
com
+- APP
+- Application.java <--- your main class should be here, above your controller classes
|
+- model
| +- user.java
+- controller
+- UserController.java
com
+-应用程序
+-Application.java在我的例子中,由于包的位置,这意味着控制器的包必须位于主类包之上
如果我的主类包是包co.companyname.spring.tutorial代码>任何控制器包都应该包co.companyname.spring.tutorial.WHAT\u EVER\u在此代码>
package co.companyname.spring.tutorial; // package for main class
@SpringBootApplication
public class FirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(FirstProjectApplication.class, args);
}
}
package co.companyname.spring.tutorial.controllers; // package for controllers
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello, world";
}}
完成编码后,按启动仪表板
最后一件事是确保您的控制器正在映射,而不仅仅是控制台,您应该看到一些微笑
Mapped "{[/hello]}" onto public java.lang.String co.companyname.spring.tutorial.controllers.HelloController.hello()
快乐编码尝试添加依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
弹簧启动装置
我为解决这类问题所做的一切就是在MVCConfig类中提到anotion@Configuration
像这个:
package com.example;
/**
* Created by sartika.s.hasibuan on 1/10/2017.
*/
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
当我们创建一个springboot应用程序时,我们用@SpringBootApplication
注释对其进行注释。此注释“封装”了应用程序工作所需的许多其他注释。一个这样的注释是@ComponentScan
注释。此注释告诉Spring查找Spring组件并配置应用程序以运行
您的应用程序类需要位于包层次结构的顶部,以便Spring可以扫描子包并找到其他必需的组件
package com.test.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
下面的代码片段在控制器包位于com.test.spring.boot
包下时起作用
package com.test.spring.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
下面的代码段不起作用,因为控制器包不在com.test.spring.boot
包下
package com.test.spring.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
从Spring引导文档:
许多Spring Boot开发人员总是对其主类进行注释
使用@Configuration
,@EnableAutoConfiguration
和@ComponentScan
。
因为这些注释经常一起使用(特别是在
如果您遵循上面的最佳实践),Spring Boot将提供
方便的@springbootplication
替代方案
@springbootplication
注释相当于使用
@配置
,@EnableAutoConfiguration
和@组件扫描
默认属性
我添加了这个依赖项,它解决了我的问题
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
弹簧启动装置
我有一个类似的错误,我使用了spring引导和velocity,我的解决方案是检查文件application.properties,spring.velocity.toolbox-config-location,发现这个属性是错误的
“此应用程序没有/error的显式映射,因此您将其视为一种回退。”
这是因为它不会像这样扫描您必须在main()类中指定的控制器和服务类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
**@ComponentScan({"com.example.demo", "controller", "service"})**
public class SpringBootMvcExample1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootMvcExample1Application.class, args);
}
}
注意:在这里,我指定了要扫描的各种类,如demo、controller和service,只有这样它才能正常工作。在我的例子中,在使用maven首先运行SpringApplication之后,从IntelliJ中运行SpringApplication时,会出现此问题
为了解决这个问题,我首先运行mvnclean
。然后我从IntelliJ内部运行SpringApplication 在我的例子中,controller类被注释为@controller
。将其更改为@RestController
解决了问题。
基本上,@RestController
是@Controller+@ResponseBody
因此,对每个方法使用@RestController
,或@Controller
和@ResponseBody
注释
这里有一些有用的注意事项:在控制器类中将@Controller更改为@RestController,一切都会顺利进行。我也遇到了同样的错误,并且能够通过将以下依赖项添加到我的pom.xml中来解决错误
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
org.apache.tomcat.embed
汤姆卡特·贾斯珀
假如
原因是我们使用JSP作为视图。Spring引导的默认嵌入式servlet容器
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
@RestController
public class EmployeeController {
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@RequestMapping(value = "/employee/save", method = RequestMethod.GET)
public String save(){
Employee newEmp = new Employee();
newEmp.setAge(25);
newEmp.setFirstName("Pikachu");
newEmp.setId(100);
return "Name: " + newEmp.getFirstName() + ", Age: " + newEmp.getAge() + ", Id = " + newEmp.getId();
}
}
@SpringBootApplication
@ComponentScan
public class CommentStoreApplication {
public static void main(String[] args) {
SpringApplication.run(CommentStoreApplication.class, args);
}
}
@RestController
public class CommentStoreApp {
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
}
@RestController("/endpoint")
public class EndpointController {
@RestController
@RequestMapping("/endpoint")
public class EndpointController {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
**@Configuration**
public class MvcConfig implements WebMvcConfigurer{
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/").setViewName("login");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/dashboard").setViewName("dashboard");
}
}
package com.rumango.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController implements ErrorController{
private final static String PATH = "/error";
@Override
@RequestMapping(PATH)
@ResponseBody
public String getErrorPath() {
// TODO Auto-generated method stub
return "No Mapping Found";
}
}
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = {"your package 1", "your package2"})
public class CommentStoreApplication {
public static void main(String[] args) {
SpringApplication.run(CommentStoreApplication.class, args);
}
}
@SpringBootApplication(scanBasePackages = {"com.module.restapi1.controller"})
package com.module.restapi;
package com.module.restapi.controller