为什么80%的码农都做不了架构师?>>>
在完成controller编码之后测试时发现,内部的接口调用不通,代码结构如下
启动类代码如下:
@SpringBootApplication
@PropertySource("classpath:application.yml")
@PropertySource("classpath:application-datasource.yml")
@PropertySource("classpath:application-druid.yml")
@EnableSwagger2
@RestController
public class IndexApplication {
private static Logger logger = Logger.getLogger(IndexApplication.class);
@RequestMapping("/")
//需要返回json数组的在class上添加
String home() {
return "Hello World!";
}
/*@RequestMapping("/index")
//需要跳转到页面需要在class上添加@Controller
public String index(Model model) {
model.addAttribute("msg", "welcome you!");
return "index";
}*/
public static void main(String[] args) throws Exception{
SpringApplication.run(IndexApplication.class, args);
logger.info("======spring boot start success ===========");
}
@Bean
public PropertySourcesPlaceholderConfigurer getSources() {
return new PropertySourcesPlaceholderConfigurer();
}
}
controller代码如下:
//@Api:用在类上,说明该类的作用
@Api(value = "XX业务接口类", tags = "XX业务接口", description = "主要XXX作用")
//@ApiResponses:用于表示一组响应
@ApiResponses(value = {
@ApiResponse(code = 200, message = "请求正常完成"),
@ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
@ApiResponse(code = 401, message = "未授权客户机访问数据"),
@ApiResponse(code = 403, message = "服务器接受请求,但是拒绝处理"),
@ApiResponse(code = 404, message = "服务器找不到给定的资源,文档不存在"),
@ApiResponse(code = 500, message = "服务器出现异常")}
/* @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如”请求参数没填好”
response:抛出异常的类*/
)
@RestController//@Controller + @ResponseBody
@RequestMapping(value = "tempA")
public class TempAController {
private static Logger logger = Logger.getLogger(TempAController.class);
// @ApiOperation:用在方法上,说明方法的作用
@ApiOperation(value = "检查用户账号接口", notes = "检查该用户是否在6合1系统中存在", produces = "application/json")
@RequestMapping(value = "/checkTempA", method = RequestMethod.POST)
public ResponseEntity checkTempA(@RequestBody String req) {
logger.info("check star...");
String result = req;
return new ResponseEntity(result, HttpStatus.OK);
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "tempA";
}
}
这里发现tempA并没有启动,然而这里有个意外发现tempB启动了,而tempB的路径和启动类是同一级的,这时候可以猜想,是否spring-boot默认加载的是其当前路径及其子路径呢,答案是必然的。官方建议启动类和controller包在同一级目录。
之后往这个方向去查询,引入了@ComponentScan这个东西,这个注解告诉Spring哪个packages的用注解标识的类(@RestController//@Controller + @ResponseBody
)会被spring自动扫描并且装入bean容器,所以真相大白。也得出一个小知识
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan