从spring boot官网下载了一个demo,然后通过eclipse导入。
参考的链接:构建微服务:Spring boot 入门篇
启动spring boot 项目后,在浏览器输入:http://localhost:8080 ,报错:
然而,这似乎是正常的。。。
新建一个controller类。项目结构目录如下:
对应的代码:
package hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/server/zwh")
public class HelloWorldController {
@RequestMapping(value = "/helloo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public String hello(){
return "hello";
}
}
启动项目,还是报上面的404错误。
后面参考的资料说:只会执行Application同包下的子包和类。
调整项目结构如下,test1---类和Application同级:
启动项目,浏览器输入:http://localhost:8080/server/zwh/hello
输出结果正常:
test2---类所在的包和Application同级(项目一般都要建子包,划分mvc的结构)
代码:
package com.project.springBoot.serverWeb;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/server/zwh")
public class GoodWorldController {
@RequestMapping(value = "/good", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public String hello(){
return "good";
}
}
项目启动后,输入:http://localhost:8080/server/zwh/good
结论:正常启动spring boot 项目去验证需要有一个controller层。另外还要跟Application.java在同一个包下。
补充:在新建子包时候,新建的包跟com.project.springBoot处于同一级。修改配置以及建包的参考链接如下: