• springboot写一个简单的接口样例


    1、重新创建一个maven的工程,之前maven的配置在前面文章都已描述:

    之后替换pom.xml的内容

    具体xml内容如下,如果找不到版本可以到镜像仓库去查一下:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.3.0.RELEASE</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.kaven</groupId>
    12. <artifactId>springboot</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>springboot</name>
    15. <description>springboot</description>
    16. <properties>
    17. <java.version>1.8</java.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-web</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-test</artifactId>
    27. <scope>test</scope>
    28. </dependency>
    29. </dependencies>
    30. <build>
    31. <plugins>
    32. <plugin>
    33. <groupId>org.springframework.boot</groupId>
    34. <artifactId>spring-boot-maven-plugin</artifactId>
    35. </plugin>
    36. </plugins>
    37. </build>
    38. </project>

     2、创建简单的类

    在java目录下创建一个package

    com.k8s.springboot

    创建SpringbootApplication类,里面用于启动服务

     

    1. package com.k8s.springboot;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class SpringbootApplication {
    6. public static void main(String[] args) {
    7. SpringApplication.run(SpringbootApplication.class, args);
    8. }
    9. }

    之后再同一级目录下创建controller文件夹,并创建HelloWorldController类文件:

    1. package com.k8s.springboot.controller;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class HelloWorldController {
    6. @GetMapping("/helloworld")
    7. public String helloWorld() {
    8. return "Hello World";
    9. }
    10. }

    创建好后直接运行 

     

    之后再访问 http://localhost:8080/helloworld

     

     初学java,也是参考了很多网上文章,希望对大家有帮助

  • 相关阅读:
    10 【Express基本使用】
    CDB与OA-以T为例子进行分析
    图像实时采集系统
    最新版 Let’s Encrypt免费证书申请步骤,保姆级教程
    C++ STL进阶与补充(list容器)
    【玩转Springcloud Alibaba系列】使用Nacos 实现服务注册与负载均衡
    Redis 和 Memcache 的区别
    Real-Time Rendering——9.11 Wave Optics BRDF Models波动光学BRDF模型
    SQL Server数据库查询优化
    散文:dflow 是如何实现slice的
  • 原文地址:https://blog.csdn.net/m0_37570494/article/details/125482677