1.创建一个hello springboot的web应用

目标;当地址栏http://localhost:8080/hello时,页面可以输出hello springboot

前提:maven使用的是3.3版本以上,jdk在maven配置文件中指定版本为1.8
    <profile>
         <id>jdk-1.8</id>
         <activation>
              <jdk>1.8</jdk>
              <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
          <maven.compiler.source>1.8</maven.compiler.source>
          <maven.compiler.target>1.8</maven.compiler.target>
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
   </profile>
1.创建一个maven应用
2.编写主类程序:加上@SpringBootApplication标签来标明该类是springboot的主程序类(程序入口)
    /**
     * springboot的主程序类
     * @SpringBootApplication:这是一个springBoot的应用
     */
    @SpringBootApplication
    public class MainApplication {
        public static void main(String[] args) {
            //启动程序:传入该类名和方法入参
            SpringApplication.run(MainApplication.class, args);
        }
    }
3.编写业务逻辑
    @Controller
    public class HelloController {
        @RequestMapping("/hello")
        @ResponseBody----->加上该标签,会将string直接返回给浏览器,不需要走视图解析器逻辑
        public String handleHello(){
            return "hello springboot";
        }
    }
    也可以将上面的注解合并为:
        @RestController----->可以将@ResponseBody标签和@Controller合并为一个标签:@RestController
        public class HelloController {
            @RequestMapping("/hello")
            public String handleHello(){
                System.out.println("处理hello请求,返回hello springboot!");
                return "hello springboot";
            }
        }
    详解@RestController标签:发现其里面包含了@Controller,@ResponseBody标签
        @Target({ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Controller
        @ResponseBody
        public @interface RestController {
            @AliasFor(
                annotation = Controller.class
            )
            String value() default "";
        }
        
4.配置文件的添加:application.properties
    在配置文件中可以指定tomcat的默认端口号等等配置:
    

5.在pom.xml添加打包方式,可以将其达成可运行的jar包
    在dependencies下发加上:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
问题:为什么jar包却可以使用tomcat等

posted @ 2022-05-09 20:37  努力的达子  阅读(44)  评论(0编辑  收藏  举报