SpringBoot接口开发

一、springboot官方demo开发

  1. 依赖包和父:pom.xml

    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.14
    
    
        org.springframework.boot
        spring-boot-starter-web
        2.7.14
    

  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. 运行结果

    SpringBoot接口开发_第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

你可能感兴趣的:(技术分享,spring,boot,后端,java)