Spring Boot : Whitelabel Error Page解决方案

 

楼主最近爱上了一个新框架——Spring Boot, 搭建快还不用写一堆xml,最重要的是自带Tomcat 真是好
pom.xml
复制代码
1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.demo</groupId>
 7     <artifactId>Demo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>Demo</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.9.RELEASE</version>
18         <relativePath /> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         
29         <dependency>
30             <groupId>org.mybatis.spring.boot</groupId>
31             <artifactId>mybatis-spring-boot-starter</artifactId>
32             <version>1.3.1</version>
33         </dependency>
34         
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-web</artifactId>
38         </dependency>
39 
40         <dependency>
41             <groupId>org.springframework.boot</groupId>
42             <artifactId>spring-boot-starter-jdbc</artifactId>
43             <exclusions>
44                 <exclusion>
45                     <groupId>org.apache.tomcat</groupId>
46                     <artifactId>tomcat-jdbc</artifactId>
47                 </exclusion>
48             </exclusions>
49         </dependency>
50 
51         <dependency>
52             <groupId>org.springframework.boot</groupId>
53             <artifactId>spring-boot-starter-test</artifactId>
54             <scope>test</scope>
55         </dependency>
56         
57     </dependencies>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65         </plugins>
66     </build>
67 
68 </project>
复制代码
controller.java
复制代码
 1 package com.demo.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 import com.demo.domain.User;
 9 import com.demo.service.UserService;
10 
11 @Controller
12 @RequestMapping(value = "/uc")
13 public class UserController {
14     
15     @Autowired
16     private UserService userService;
17 
18     @ResponseBody
19     @RequestMapping("/stemp.htm")
20     private String sYeMian(User user){
21         System.out.println("进了方法 syso");
22         
23         User u = new User();
24         u.setMuBan(user.getMuBan());
25         
26         int i = userService.sYeMian(u);
27         
28         if (i>0){
29             return "存储成功";
30         }
31             return "存储失败";
32     }
33 }
复制代码
DemoApplication.java
复制代码
 1 package com.demo.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Demo1Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Demo1Application.class, args);
11     }
12 }
复制代码
然而运行之后就懵逼了
这是咋了,咋的就404了 我路径也挺对的啊 注解也都写上了啊 咋就找不到了呢? debug吧它不进方法 看日志吧,他还不报错 这家伙给我急的 百度一下午也没解决,最后还是看官网才知道错在了那里,程序只加载Application.java所在包及其子包下的内容。
解决方案:
一、在Application类中加上@ComponentScan(basePackages = {"com.demo.controller"}) 多个之间用","分隔 当然,这样治标不治本
复制代码
 1 package com.demo.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.ComponentScan;
 6 
 7 @SpringBootApplication
 8 @ComponentScan(basePackages = {"com.demo.controller"})
 9 public class Demo1Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(Demo1Application.class, args);
13     }
14 }
复制代码
二、把包的目录结构修改成正确的
复制代码
 1 package com.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.ComponentScan;
 6 
 7 @SpringBootApplication
 8 public class Demo1Application {
 9 
10     public static void main(String[] args) {
11         SpringApplication.run(Demo1Application.class, args);
12     }
13 }
复制代码

 

posted @ 2018-06-19 17:23  割肉机  阅读(28000)  评论(3编辑  收藏  举报