比較常見就是這兩種
@GetMapping
@PostMapping
這兩種又有
@RequestMappin
此文章沒討論用這個**@RequestMappin** 這算是比較舊的寫法,或許後續有空會補上
通常我們呼叫 restAPi可以用
@GetMapping
@PostMapping
那麼又有分
就掉用來講我們把
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaServiceProviderApplication {
@Value("${server.port}")
String port;
@GetMapping("/")
public String home(@RequestParam (value="id", required = false) Integer employeeId) {
return "Hello world" + port+ employeeId;
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceProviderApplication.class, args);
}
}
@FeignClient("eureka-provider")
public interface HomeClient {
@GetMapping("/")
public String home(@RequestParam (value="id", required = false) Integer employeeId) ;
}
@RestController
public class ConsumerController {
@Autowired
private HomeClient homeClient;
@GetMapping("/hello/{id}")
public String hello(@PathVariable(name="id") Integer employeeId) {
System.out.print(employeeId);
return homeClient.home(employeeId);
}
}
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaServiceProviderApplication {
@Value("${server.port}")
String port;
@GetMapping("/")
public String home(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) {
return "Hello world" + port+ employeeId+employeeId2;
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceProviderApplication.class, args);
}
}
@FeignClient("eureka-provider")
public interface HomeClient {
@GetMapping("/")
public String home(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) ;
}
@RestController
public class ConsumerController {
@Autowired
private HomeClient homeClient;
@GetMapping("/hello/{id}/{id2}")
public String hello(@PathVariable(name="id") Integer employeeId,@PathVariable(name="id2") Integer employeeId2) {
System.out.print(employeeId2);
return homeClient.home(employeeId,employeeId2);
}
}
詳細可以參考
SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍
http://www.itmuch.com/spring-cloud-sum/feign-multiple-params/
構建一個 entity
UserEntity.class
這邊好像在一個網站有看過這邊大小寫 windows 沒有限制,在linux 就嚴格 限制 大小寫,後面會來好好講解,
然後這個 Entity 同時在 Provider 和 Consumer 裡。
public class UserEntity {
private Integer id;
private String Name ;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
public class UserEntity {
private Integer id;
private String Name ;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@FeignClient("eureka-provider")
public interface HomeClient {
@GetMapping("/")
public String home(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) ;
@PostMapping("/user")
public String aveUser(@RequestBody UserEntity user);
}
@RestController
public class ConsumerController {
@Autowired
private HomeClient homeClient;
@GetMapping("/hello/{id}/{id2}")
public String hello(@PathVariable(name="id") Integer employeeId,@PathVariable(name="id2") Integer employeeId2) {
System.out.print(employeeId2);
return homeClient.home(employeeId,employeeId2);
}
@PostMapping("/user")
public String aveUser(@RequestBody UserEntity user) {
return homeClient.aveUser(user);
}
}
注意大小寫
大概變化就這幾種
接下來就輪到介紹我們的 Hystrix 斷路器。
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing