springboot 启动后通过网页访问出现如下错误:http://localhost:9999/user/
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jan 04 11:16:27 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
原因是 包的命名问题
例如 你的 包命名为 com.julongtech.main springboot启动类位于此包下
package com.julongtech.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class ProviderUserApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
如果你重新定义了一个 action 包 名字是 com.julongtech.action 这样子
package com.julongtech.action;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.julongtech.entity.UserInfo;
@RestController
public class UserAction {
@RequestMapping(value="/user")
public String home() {
return "Hello World!";
}
@RequestMapping(value="/user/{userId}")
public UserInfo findById(@PathVariable String userId){
UserInfo user = new UserInfo();
user.setUserId("A0001");
user.setUserName("测试数据");
return user;
}
}
这样子 启动就不会扫描自己创建的包,默认springboot启动扫描的是 启动类所在的包的和他的子包
解决方法
1.将springboot启动类 直接由com.julongtech.main 包放到 com.julongtech包下即可解决
2.自定义扫描的包在启动类加入 @ComponentScan(basePackages={"com.julongtech"})
package com.julongtech.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@ComponentScan(basePackages={"com.julongtech"})
public class ProviderUserApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderUserApplication.class, args);
}
}