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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 "用户名或密码错误";

    }

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值