“OpenFeign 传递 RESTFul 参数”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1My4y1W7vy?p=5”的新页面) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的6个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1My4y1W7vy?p=5 | https://www.bilibili.com/video/BV1My4y1W7vy?p=5 | ||
+ | |||
+ | === 被调用的 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("/demo3/{name}/{age}") | ||
+ | public String demo3(@PathVariable String name, @PathVariable Integer age) { | ||
+ | return "name:" + name + ",age:" + age; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 调用方 application client === | ||
+ | |||
+ | ==== ServiceDemoFeign.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.feign; | ||
+ | |||
+ | 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 { | ||
+ | |||
+ | @RequestMapping("/demo3/{jqk}/{abc}") | ||
+ | String suiyi3(@RequestParam String jqk, @RequestParam Integer age); | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight>下面这么写也是可以的:<syntaxhighlight lang="java"> | ||
+ | @RequestMapping("/demo3/{jqk}/{abc}") | ||
+ | String suiyi3(@PathVariable String jqk, @PathVariable Integer abc); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== FeignDemoService.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.service; | ||
+ | public interface FeignDemoService { | ||
+ | |||
+ | String demo3(String name, Integer age); | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== FeignDemoServiceImpl.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.service.impl; | ||
+ | |||
+ | import io.github.jihch.feign.ServiceDemoFeign; | ||
+ | import io.github.jihch.service.FeignDemoService; | ||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.stereotype.Service; | ||
+ | |||
+ | @Service | ||
+ | public class FeignDemoServiceImpl implements FeignDemoService { | ||
+ | |||
+ | @Autowired | ||
+ | private ServiceDemoFeign serviceDemoFeign; | ||
+ | |||
+ | @Override | ||
+ | public String demo3(String name, Integer age) { | ||
+ | return serviceDemoFeign.suiyi3(name, age); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== FeignDemoController.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.controller; | ||
+ | |||
+ | import io.github.jihch.service.FeignDemoService; | ||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | @RestController | ||
+ | public class FeignDemoController { | ||
+ | |||
+ | @Autowired | ||
+ | private FeignDemoService feignDemoService; | ||
+ | |||
+ | |||
+ | @RequestMapping("/demo3") | ||
+ | public String demo3() { | ||
+ | return feignDemoService.demo3("张三", 88); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </syntaxhighlight>访问:[http://localhost:8088/demo3/jihch/15 http://localhost:8088/demo3] 测试 |
2023年3月28日 (二) 06:32的最新版本
https://www.bilibili.com/video/BV1My4y1W7vy?p=5
被调用的 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("/demo3/{name}/{age}")
public String demo3(@PathVariable String name, @PathVariable Integer age) {
return "name:" + name + ",age:" + age;
}
}
调用方 application client
ServiceDemoFeign.java
package io.github.jihch.feign;
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 {
@RequestMapping("/demo3/{jqk}/{abc}")
String suiyi3(@RequestParam String jqk, @RequestParam Integer age);
}
下面这么写也是可以的:
@RequestMapping("/demo3/{jqk}/{abc}")
String suiyi3(@PathVariable String jqk, @PathVariable Integer abc);
FeignDemoService.java
package io.github.jihch.service;
public interface FeignDemoService {
String demo3(String name, Integer age);
}
FeignDemoServiceImpl.java
package io.github.jihch.service.impl;
import io.github.jihch.feign.ServiceDemoFeign;
import io.github.jihch.service.FeignDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignDemoServiceImpl implements FeignDemoService {
@Autowired
private ServiceDemoFeign serviceDemoFeign;
@Override
public String demo3(String name, Integer age) {
return serviceDemoFeign.suiyi3(name, age);
}
}
FeignDemoController.java
package io.github.jihch.controller;
import io.github.jihch.service.FeignDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignDemoController {
@Autowired
private FeignDemoService feignDemoService;
@RequestMapping("/demo3")
public String demo3() {
return feignDemoService.demo3("张三", 88);
}
}