直接响应
@GetMapping("/test5")
public Product test5(){
return new Product(1,"product",23);
}
@GetMapping("/test5")
public Product test5();
@GetMapping("/category")
public Product index(){
log.info("通过使用OpenFeign组件调用商品服务...");
Product product = productClient.test5();
log.info("product {}", product);
return product;
}
集合相应
@GetMapping("/page/{id}")
public Map<String, Object> page(@PathVariable("id") Integer id){
Map<String, Object> map = new HashMap<>();
List<Product> products = new ArrayList<>();
products.add(new Product(1,"product1",21));
products.add(new Product(2,"product2",22));
products.add(new Product(3,"product3",23));
map.put("total",101);
map.put("products", products);
return map;
}
@GetMapping("/page/{id}")
public String page(@PathVariable("id") Integer id);
引入fastjson依赖
@GetMapping("/category")
public String index(){
log.info("通过使用OpenFeign组件调用商品服务...");
String page = productClient.page(1);
JSONObject jsonObject = JSONObject.parseObject(page);
Object total = jsonObject.get("total");
log.info("总条数 {}", total);
Object products = jsonObject.get("products");
List<Product> productList = JSONObject.parseArray(products.toString(), Product.class);
productList.forEach(System.out::println);
return page;
}
**虽然productcontroller返回的是map集合,但是类上加@RestController注解返回的就是json字符串,**
**再用feginclient返回String,使用fastjson解析就可以避免map里的products集合无法转换的问题**