SpringCloud中微服务之间的调用,传递参数时需要加相应的注解。主要用到的就是这三个注解@RequestBody,@RequestParam,@PathVariable
上一篇已经展示了怎么使用@RequestParam该注解,这一篇我们使用一下剩下的两种注解:
@PathVariable使用如下:
只需要在eureka-fegin-service和eureka-fegin-service-tmp的Controller中修改如下:
package com.cn.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EurekaFeginTmpController {
@RequestMapping("/getName")
public String getName(@RequestParam String name){
return "hello2 : " + name;
}
@RequestMapping("/getName/{id}")
public String getNameById(@PathVariable Long id){
return "hello2 : " + id;
}
}
eureka-fegin的FeginService接口修改如下:
package com.cn.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-fegin-service")
public interface FeginService {
@RequestMapping(value = "/getName", method = RequestMethod.GET)
public String getName(@RequestParam("name") String name);
@RequestMapping(value = "/getName/{id}", method = RequestMethod.GET)
public String getNameById(@PathVariable("id") Long id);
}
controller修改如下:
package com.cn.controller;
import com.cn.service.FeginService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeginController {
@Autowired
private FeginService feginService;
@RequestMapping(value = "/getName")
public String search(@RequestParam("name") String name){
return feginService.getName(name);
}
@RequestMapping(value = "/getName/{id}")
public String searchById(@PathVariable("id") Long id){
return feginService.getNameById(id);
}
}
访问:http://localhost:8088/getName/100
循环输出:hello1 : 100和hello2 : 100
@RequestParam使用如下:
User类如下:
package com.cn.entity;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
只需要在eureka-fegin-service和eureka-fegin-service-tmp的Controller中添加如下:
@RequestMapping("/getUser")
public String getUser(@RequestBody User user){
return "hello2 : " + user.getName() + ", age :" + user.getAge();
}
只需要在eureka-fegin的FeginService接口中添加如下:
@RequestMapping(value = "/getUser", method = RequestMethod.POST)
public String getUser(@RequestBody User user);
controller中修改如下:
@RequestMapping(value = "/getUser")
public String searchUser(@RequestBody User user){
return feginService.getUser(user);
}
用Postman请求如下:
会循环打印上述两个图片的内容
我把eureka-fegin的FeginService接口中的请求方式换成了GET,然后postman中用get请求,依然循环输出上面两张图
PS:至于网上说的用GET请求的话,如果传递对象会报错,我试了一下发现并没有网上说的那种错误,也不知道是最新版本进行更新了,还是我用postman请求的方式不对。