SpringBoot接口开发

2023-12-15 09:27:27

一、springboot官方demo开发

  1. 依赖包和父:pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.14</version>
    </dependency>
</dependencies>
  1. 新建 SampleController.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/")
    @ResponseBody
    String home(){
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(SampleController.class,args);
    }
}
  1. 运行结果


    说明:内置了web服务器

二、使用SpringBoot开发get方法接口

返回cookie信息的get接口开发

  1. 新建Application.java 入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.course.server")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
  1. com.course.server 新建MyGetMethod.java
@RestController
public class MyGetMethod{
    @RequestMapping(value="/getCookies",method=RequestMethod.GET)
    public String getCookies(){
        return "恭喜你获得cookies信息成功";    
    }
}
  1. Resource下新建文件:application.properties
server.port=${por

文章来源:https://blog.csdn.net/xdpcxq/article/details/135008164
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。