我使用maven来编写教程https://spring.io/guides/gs/uploading-files/
我使用的所有密码都是复制的。
应用程序可以运行,但我得到了错误:
这个应用程序没有/error的显式映射,所以您认为这是一个退步。Tue 6月30日17:24:02 CST 2015发生了一个意外的错误(type=Not Found,status=404)。没有可用的消息
我怎么才能修好它?
发布于 2016-08-18 04:07:58
确保主类位于其他类之上的根包中。
当您运行Spring应用程序(即带有@SpringBootApplication注释的类)时,Spring只会扫描主类包下面的类。
com
+- APP
+- Application.java <--- your main class should be here, above your controller classes
|
+- model
| +- user.java
+- controller
+- UserController.java
发布于 2017-01-19 11:18:40
当我们创建Spring应用程序时,我们用@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!";
}
}
下面的代码片段不工作,因为控制器包不在com.test.spring.boot
包之下。
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开发人员总是使用
@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注释他们的主类。由于这些注释经常一起使用(特别是如果您遵循上面的最佳实践),Spring提供了一个方便的@SpringBootApplication
替代方案。@SpringBootApplication
注释相当于使用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
的默认属性。
发布于 2015-08-05 08:50:28
您可以通过在应用程序中添加一个ErrorController
来解决这个问题。您可以让错误控制器返回您需要的视图。
应用程序中的错误控制器如下所示:
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;
}
}
以上类基于Springs BasicErrorController类。
您可以像这样在一个ErrorController
文件中实例化上面的@Configuration
:
@Autowired
private ErrorAttributes errorAttributes;
@Bean
public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}
您可以通过实现ErrorAttributes
选择覆盖默认的ErrorAttributes。但在大多数情况下,DefaultErrorAttributes应该就足够了。
https://stackoverflow.com/questions/31134333
复制相似问题