• SpringBoot——可真是迅速又便捷


    刚工作那会用的还是tomcat、springMVC、hibernate、mybatis、html、jsp……搭个项目可真是麻烦,各种复杂的结构还得打个war包配置web.xml,启动tomcat……后来也没做网站开发了,最近又看了看springboot,比之前那种开发web项目简单多了,开发效率高;一边学习一边做笔记;

    maven导入:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot</artifactId>
    4. <version>2.2.0.RELEASE</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.boot</groupId>
    8. <artifactId>spring-boot-starter</artifactId>
    9. <version>2.2.0.RELEASE</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-parent</artifactId>
    14. <version>2.2.0.RELEASE</version>
    15. <type>pom</type>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-starter-web</artifactId>
    20. <version>2.2.0.RELEASE</version>
    21. </dependency>

    为了方便开发学习,还导入了:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-devtools</artifactId>
    4. <version>2.2.0.RELEASE</version>
    5. </dependency>

    devtools可以在一些代码变动时自动编译重启项目,也可以手动修改配置指定代码变动生效或者禁用;

    启动springboot

    在main方法里调用SpringApplication.run()方法,并传入一个class,并在这个class类前加上@SpringBootApplication注解,就可以启动一个没有controller的默认配置的web服务;(通常情况main方法也放在这个class类里启动)

    1. /**
    2. * 2022年10月20日下午3:50:18
    3. */
    4. package testspringboot.test1;
    5. import org.springframework.boot.SpringApplication;
    6. import org.springframework.boot.autoconfigure.SpringBootApplication;
    7. /**
    8. * @author XWF
    9. *
    10. */
    11. @SpringBootApplication
    12. public class Test1Main {
    13. /**
    14. * @param args
    15. */
    16. public static void main(String[] args) {
    17. SpringApplication.run(Test1Main.class, args);
    18. }
    19. }

    启动服务:

    默认端口是8080,默认项目上下文路径是“/”,没有controller访问返回错误页:

    添加配置文件

    默认配置文件名字是application.properties或者application.yml,默认可以放在四个位置:classpath/下、classpath/config/下、根目录/下、根目录/config/下;(也可以通过@PropertySource指定位置和自定义配置文件名字)

    最简单的在application.properties里指定端口和上下文路径:

    1. server.port=8888
    2. server.servlet.context-path=/testspringboot

    再次启动,端口和上下文路径改变了:

    增加controller:

    @SpringBootApplication是个组合注解,默认扫描当前类包下以及子包的bean;(也可以用@ComponentScan设置扫描路径)

    需要在controller类前加@Controller或者@RestController注解;(@RestController的controller返回字符串或者json数据,@Controller默认返回视图,在@Controller下使用@ResponseBody等同于@RestController功能)

    1. /**
    2. * 2022年10月21日下午3:01:39
    3. */
    4. package testspringboot.test1;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. /**
    8. * @author XWF
    9. *
    10. */
    11. @RestController
    12. public class Test1Controller {
    13. @RequestMapping("/test1")
    14. public String test1() {
    15. return "Test1Controller.test1()";
    16. }
    17. }

    访问结果:

    也可以在controller类前再加一个总的@RequestMapping

    1. /**
    2. * 2022年10月21日下午3:01:39
    3. */
    4. package testspringboot.test1;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. /**
    8. * @author XWF
    9. *
    10. */
    11. @RestController
    12. @RequestMapping("/Test1Controller")
    13. public class Test1Controller {
    14. @RequestMapping("/test1")
    15. public String test1() {
    16. return "HELLO";
    17. }
    18. }

  • 相关阅读:
    Android开发者在企业秋招或者社招中加分点到底有哪些?
    Java拦截所有接口请求并过滤请求头内容
    C++11单例模式
    C语言经典例题-18
    算法通关村16关 | 堆与滑动窗口问题结合
    Linux基本指令
    Java 线程安全 与 锁
    电脑异常关机录屏/软件/程序异常停止/安卓手机/数据丢失找回方案
    【Reinforcement Learning】actor-critic学习
    【VCSA 8】安装vCenter Server Appliance(VCSA) 8.0
  • 原文地址:https://blog.csdn.net/FlyLikeButterfly/article/details/127442012