“OpenFeign 传递普通表单参数”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“https://www.bilibili.com/video/BV1My4y1W7vy?p=4”的新页面)
 
第1行: 第1行:
 
https://www.bilibili.com/video/BV1My4y1W7vy?p=4
 
https://www.bilibili.com/video/BV1My4y1W7vy?p=4
 +
 +
=== 被调用的 application service ===
 +
<syntaxhighlight lang="java">
 +
import io.github.jihch.pojo.People;
 +
import org.springframework.web.bind.annotation.PathVariable;
 +
import org.springframework.web.bind.annotation.RequestBody;
 +
import org.springframework.web.bind.annotation.RequestMapping;
 +
import org.springframework.web.bind.annotation.RestController;
 +
 +
import java.util.ArrayList;
 +
import java.util.List;
 +
import java.util.Map;
 +
 +
@RestController
 +
public class DemoController {
 +
 +
    @RequestMapping("/demo2")
 +
    public String demo2(String name, Integer age) {
 +
        return "name:" + name + ",age:" + age;
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
 +
 +
=== 使用 OpenFeign 调用的 application client ===
 +
<syntaxhighlight lang="java">
 +
import org.springframework.cloud.openfeign.FeignClient;
 +
import org.springframework.web.bind.annotation.RequestMapping;
 +
import org.springframework.web.bind.annotation.RequestParam;
 +
 +
@FeignClient("MYPROJECT")
 +
public interface ServiceDemoFeign {
 +
 +
    /**
 +
    * 在 OpenFeign 中方法参数前如果没有注解,默认添加 @RequestBody 注解,最多只能存在一个不带注解的参数
 +
    *
 +
    * 普通表单参数必须添加 @RequestParam 注解。如果变量名和参数名称对应可以不写 name
 +
    *
 +
    * @return
 +
    */
 +
    @RequestMapping("/demo2")
 +
    String suiyi2(@RequestParam String name, @RequestParam Integer age);
 +
 +
}
 +
</syntaxhighlight>

2023年3月28日 (二) 03:26的版本

https://www.bilibili.com/video/BV1My4y1W7vy?p=4

被调用的 application service

import io.github.jihch.pojo.People;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {

    @RequestMapping("/demo2")
    public String demo2(String name, Integer age) {
        return "name:" + name + ",age:" + age;
    }

}


使用 OpenFeign 调用的 application client

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("MYPROJECT")
public interface ServiceDemoFeign {

    /**
     * 在 OpenFeign 中方法参数前如果没有注解,默认添加 @RequestBody 注解,最多只能存在一个不带注解的参数
     *
     * 普通表单参数必须添加 @RequestParam 注解。如果变量名和参数名称对应可以不写 name
     *
     * @return
     */
    @RequestMapping("/demo2")
    String suiyi2(@RequestParam String name, @RequestParam Integer age);

}