标签:结构 ack ring otf 开启 map blog model author
简介spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.honghh</groupId>
<artifactId>boot-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-first</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.honghh.bootfirst.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: HelloWordController
* Description:
*
* @author honghh
* @date 2019/02/19 15:58
*/
@RestController
public class HelloWordController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot!";
}
}
启动项目,在浏览器中输入: http://localhost:8080/
启动成功,第一个springboot项目搭建成功!
但是这个要注意一个点,现在我的controller是写在com.honghh.bootfirst下的,所以没有问题,我们将controller包放在com.honghh.controller下执行你会发现报404
那我们应该怎么解决呢?
Spring Boot 正常启动后访问Controller提示404
以启动类的包路径作为顶层包路径,列如启动类包为com.honghh.bootfirst,那么Controller包路径就为com.honghh.bootfirst.controller。
在启动上方添加@ComponentScan注解,此注解为指定扫描路径,例如:
@ComponentScan(basePackages = {"com.honghh.*"}) #多个不同的以逗号分割。
package com.honghh.bootfirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.honghh.*"})
@SpringBootApplication
public class BootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(BootFirstApplication.class, args);
}
}
文章来源: https://blog.csdn.net/qq_35098526/article/details/87715317
标签:结构 ack ring otf 开启 map blog model author
原文地址:https://blog.51cto.com/11681903/2355849