使用SpringBoot实现get和post请求接口

本文介绍了如何使用SpringBoot配置固定端口号,创建启动类,以及实现RESTful API接口,包括GET方法中添加Cookie、携带Cookie访问、携带参数访问。同时展示了POST接口的实现,涉及登录及获取Cookie的场景。
摘要由CSDN通过智能技术生成

1、统一端口号

在resources文件夹下新建application.properties文件

添加以下一行,固定端口号为8888

server.port=${port:8888}

2、Springboot启动类

@SpringBootApplication标签代表启动类,main方法

添加@ComponentScan标签,代表扫描哪个包下的类

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;



@SpringBootApplication

@ComponentScan("com.example")

public class DemoApplication {


    public static void main(String[] args) {

        SpringApplication.run(com.example.demo.DemoApplication.class, args);

    }

}

3、get方法

在类上加上@RestController标签

@RestController的作用等同于@Controller + @ResponseBody

(1)在response中添加cookie

@RequestMapping(value = "/getCookies",method = RequestMethod.GET)

public String getCookies(HttpServletResponse response){

    //HttpServerletRequest装请求信息的类

    //HttpServerletResponse装响应信息的类

    Cookie cookie = new Cookie("login", "true");

    response.addCookie(cookie);

    return "恭喜获得cookie成功";

}

(2)客户端request携带cookie进行访问

/*

* 要求客户端携带cookies访问

* 这是一个需要携带cookies信息才能访问的get请求

* 使用jmeter,设置好cookie,发送get请求成功

* */

@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)

public String getWithCookies(HttpServletRequest request){

    Cookie[] cookies = request.getCookies();

    if(Objects.isNull(cookies)){

        return "你必须携带cookies信息来";

    }

    for(Cookie cookie : cookies){

        if(cookie.getName().equals("login") &&

                cookie.getValue().equals("true")){

            return "这是一个需要携带cookies信息才能访问的get请求";

        }

    }

    return "你必须携带cookies信息来";

}

(3)需要携带参数访问的get请求,两种方式

第一种方式:参数加@RequestParam标签,@RequestMapping(value = “uri” , method = RequestMethod.GET)

第二张方式:参数加@PathVariable标签,@RequestMapping(value = “uri/{参数名}/{参数名}”)

/*

* 开发一个需要携带参数才能访问的get请求

* 第一种实现方式 url:ip:port/get/with/param?key=value&key=value

* 我们来模拟获取商品列表

* */

@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)

public Map<String,Integer> getList(@RequestParam Integer start,

                                   @RequestParam Integer end){

    Map<String,Integer> myList = new HashMap<>();

    myList.put("鞋",400);

    myList.put("干脆面",1);

    myList.put("衬衫",300);

    return myList;

}


/*

* 第二种需要携带参数访问的get请求的实现方式

* url:ip:port/get/with/param/10/20

* */

@RequestMapping(value = "/get/with/param/{start}/{end}")

public Map<String,Integer> getList2(@PathVariable Integer start,

                                    @PathVariable Integer end){

    Map<String,Integer> myList = new HashMap<>();

    myList.put("鞋",400);

    myList.put("干脆面",1);

    myList.put("衬衫",300);

    return myList;

}

4、使用SpringBoot实现post接口

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;



import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletResponse;



@RestController

@Api(value = "/",description = "这是全部的post请求")

@RequestMapping("v1")

public class MyPostMethod {



    //这个变量是用来装我们cookie信息的

    private static Cookie cookie;



    //用户登录成功获取到cookies,然后再访问其他接口获取到列表

    @RequestMapping(value = "/login",method = RequestMethod.POST)

    @ApiOperation(value = "登录接口成功后获取到cookie",httpMethod = "POST")

    public String login(HttpServletResponse response,

                        @RequestParam(value = "userName",required = true) String userName,

                        @RequestParam(value = "password",required = true) String password){

        if(userName.equals("zhangssan")&& password.equals("123456")){

            cookie = new Cookie("login", "true");

            response.addCookie(cookie);

            return "恭喜登录成功";

        }

        return "用户名或密码错误";

    }

}

 

### 回答1: Spring Boot中的GET和POST请求是常见的HTTP请求方法,用于获取或提交数据。 GET请求用于从服务器获取数据,通常用于查询数据。在Spring Boot中,可以使用@GetMapping注解来处理GET请求POST请求用于向服务器提交数据,通常用于创建或更新数据。在Spring Boot中,可以使用@PostMapping注解来处理POST请求。 除了@GetMapping和@PostMapping,Spring Boot还提供了其他注解来处理不同类型的HTTP请求,例如@PutMapping用于处理PUT请求,@DeleteMapping用于处理DELETE请求等。 ### 回答2: Spring Boot的Web应用程序通常会使用HTTP协议进行与客户端的通信。在HTTP协议中,最常见的两种请求类型是GET和POST请求。 GET请求: GET请求用于从服务器获取数据。当客户端发送GET请求时,服务器会将请求的资源作为响应发送回客户端。 GET请求将参数作为URL的一部分发送,而不是在请求主体中发送,这说明查询参数可以缓存,字符串鉴别标识符可以保护请求: @GetMapping("/users") public List<User> getUsers() { return userService.getUsers(); } 上面的示例使用@GetMapping注释将方法映射到“/users”URL路径。当客户端发送GET请求到“/users”时,将调用该方法并返回所有用户的列表。 POST请求POST请求用于向服务器提交数据。使用POST请求时,请求的数据在请求主体中发送而不是作为URL的一部分发送。这意味着数据不会被缓存在浏览器中,因此POST请求的安全性要高于GET请求。 @PostMapping("/users") public User createUser(@RequestBody User user) { return userService.createUser(user); } 上面的示例使用@PostMapping注释将方法映射到“/users”URL路径。当客户端发送针对该路径的POST请求时,将调用该方法并创建新用户,然后将其添加到用户列表中。 总而言之,GET请求适用于无需更新服务端上的资源且不会引起副作用的场景。POST请求则用于需要向服务器传递数据的场景,并且可能会更新或创建新资源。 ### 回答3: Spring Boot 是一个用于构建企业级应用的 Java 框架,它的出现简化了 Java 开发的流程,使得开发者可以以更加高效和规范的方式进行开发。在 Spring Boot 中,Get 和 Post 请求是两个最基本的网络请求方式。 Get 请求请求一个指定的页面或资源,一般来说是在地址栏中输入一个 URL,并向该 URL 发送请求。Get 请求通常用于获取一些数据或资源,比如查询数据库的信息或者获取某个页面的文本内容等。在 Spring Boot 中,可以使用 @GetMapping 注解来进行 Get 请求的处理,它可以将请求和指定的方法进行绑定,使得请求能够在服务器端得到处理并返回响应。 例如,在 Spring Boot 中,使用 @GetMapping 注解可以实现一个简单的 Get 请求处理器: ``` @GetMapping("/hello") public String hello() { return "Hello World!"; } ``` 上面的示例中,GetMapping 注解绑定了一个地址为 /hello 的请求,并返回了一个 Hello World! 字符串作为响应。 与 Get 请求不同,Post 请求则是向服务器提交数据以在服务器端进行处理。Post 请求通常用于提交数据或资源,如向数据库中添加新的记录或向服务器上传文件等。在 Spring Boot 中,可以使用 @PostMapping 注解来进行 Post 请求的处理,它可以将请求和指定的方法进行绑定,使得请求能够在服务器端得到处理并返回响应。 例如,在 Spring Boot 中,使用 @PostMapping 注解可以实现一个简单的 Post 请求处理器: ``` @PostMapping("/submit") public String submitForm(@RequestParam String username, @RequestParam String password) { // 处理提交的数据 return "Success"; } ``` 上面的示例中,PostMapping 注解绑定了一个地址为 /submit 的请求,并通过 @RequestParam 注解声明了两个参数 username 和 password,处理提交的数据并返回了一个 Success 字符串作为响应。 总之,Spring Boot 中的 Get 和 Post 请求是处理客户端与服务端之间数据传输的最基本方式。开发者在使用这两种请求时需要熟练掌握它们的基本原理和使用方法,才能更好地实现涉及网络请求的应用程序。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值