4.使用swagger实现post请求

专栏目录

https://blog.csdn.net/xiaobai_july/category_10377198.html

一、编写一个post请求的方法

接收两个字符参数,定义的类型要和你数据库的类型对应。

    @PostMapping(value = "/add",produces = "text/html; charset=UTF-8")
    public String InsertData(String t1,String t2){
        service.addData(t1,t2);
        return "成功";
    }

编写service方法

    @Autowired
    private TestMapper mapper;
    public void addData(String t1, String t2) {
        mapper.addData(t1,t2);
    }

创建mapper接口方法
使用@Param注解,就不需要在xml文件设置传给数据的参数类型了,直接拿@Param里面的名字就可以传参,该注解需要在mapper层编写,service层不会生效

void addData(@Param("t1") String t1,@Param("t2") String t2);

最后编写xml文件内容
该id和接口的方法名对应,不然不会执行该数据库语句
#{t1},#{t2}对应@Param中的名字

    <insert id="addData">
        insert into test values (#{t1},#{t2})
    </insert>

二、在swagger.html进行post提交

访问swagger的默认地址http://localhost:8080/swagger-ui.html
(后续不在强调该地址)
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用`restTemplate`添加请求body时,可以使用以下几种方法: 1. 使用`postForObject`方法发送POST请求并获取响应结果,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`作为参数传入`postForObject`方法中。例如: ``` MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("dt", "20190225"); String result = template.postForObject(url, paramMap, String.class); ``` 2. 使用`postForEntity`方法发送POST请求并获取响应结果。首先创建`HttpHeaders`和`HttpEntity`对象,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`和`headers`作为参数传入`HttpEntity`构造函数中,最后将`HttpEntity`对象作为参数传入`postForEntity`方法中。例如: ``` HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class); ``` 3. 使用`exchange`方法发送POST请求并获取响应结果。首先创建`HttpHeaders`和`HttpEntity`对象,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`和`headers`作为参数传入`HttpEntity`构造函数中,最后将`HttpEntity`对象作为参数传入`exchange`方法中。例如: ``` HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class); ``` 以上是使用`restTemplate`添加请求body的几种方法,根据具体情况选择适合的方法进行使用。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [RestTemplate 添加请求头headers和请求体body](https://blog.csdn.net/u011138533/article/details/87920632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [spring-boot-swagger.7z](https://download.csdn.net/download/bethzhang/11349186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我柒月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值