美文网首页
学习springboot(一)——helloworld

学习springboot(一)——helloworld

作者: 华木公子 | 来源:发表于2021-08-11 16:46 被阅读0次

    1、设置好idea环境
    2、设置好maven环境
    3、创建一个hello的maven工程;
    4、修改pom,添加spring boot依赖:
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    </parent>
    上述内容用于引入spingboot所有需要的基础依赖;

    5、添加main 类;
    6、在main类上添加 @SpringBootApplication 以告诉系统这是个spring boot应用
    7、在main方法里,添加 SpingApplication.run(A.class, args);
    如:

    @SpringBootApplication
    public class Myboot {
    public static void main(String[] args){
    SpringApplication.run(Myboot.class, args);
    }
    }

    以上,完成了一个spring boot的搭建;通过edit - configragiton ,以spring boot模板启动,即可;
    以下,是需要通过浏览器访问spring boot程序并返回值,需要添加web依赖(也即是SPRING MVC的依赖);
    8、 <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
    上述内容用于引入spingboot所有需要的web依赖;
    9、编写一个Hello类,添加@Controller注解
    添加say方法,添加 @RequestMapping,设置返回@Responsebody,如:

    @Controller
    public class HellowordController {

    @RequestMapping("/hello")
    public @ResponseBody String sayHello(){
        return "hello world";
    }id
    
    @RequestMapping("/hello/{id}")
    public @ResponseBody String say(@PathVariable String id){
        return "hello world";
    }
    

    }

    10、启动程序,通过浏览器访问 127.0.0.1/hello 即可看到浏览器呈现 hello world

    相关文章

      网友评论

          本文标题:学习springboot(一)——helloworld

          本文链接:https://www.haomeiwen.com/subject/yzhzjltx.html