我用RestTemplate想两个参数,一个是自己写的Java类,一个是基本类型
请求的Controller
@RequestMapping("/buy")
public int buy(@RequestBody GoodsDetail goodsDetail,Integer buyCount){
goodsDetail.setRemaining(goodsDetail.getRemaining() - buyCount);
int num = goodsDetailMapper.updateByPrimaryKey(goodsDetail);
return num;
}
用getForObject:
Map<String,Object> map = new HashMap<>();
map.put("goodsDetail",goodsDetail);
map.put("buyCount",buyCount);
int num = restTemplate.getForObject("http://localhost:8088/shop-buy/buy?goodsDetail={goodsDetail}&buyCount={buyCount}", int.class, map);
报400 null
用postForObject:
MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("goodsDetail",goodsDetail);
multiValueMap.add("buyCount",buyCount);
int num = restTemplate.postForObject("http://localhost:8088/shop-buy/buy", multiValueMap, int.class);
报415 null
求救,要疯掉了!
@RequestParam 或者将这两个封装到一个实体类
@RequestMapping("/buy/{buyCount}")
public int buy(@RequestBody GoodsDetail goodsDetail,@PathParam ("buyCount") Integer buyCount){
goodsDetail.setRemaining(goodsDetail.getRemaining() - buyCount);
int num = goodsDetailMapper.updateByPrimaryKey(goodsDetail);
return num;
}
restTemplate 好像无法既传方法体,又传地址栏。只能传方法体的同时传restfull风格的变量,比如
//无法访问这样的接口
@PostMapping("/hello/")
public R hello(@RequestBody StudentVo vo, @RequstParam("id") Interge id){
}
//对于这样的接口可以访问
@PostMapping("/hello/{id}")
public R hello(@RequestBody StudentVo vo, @PathVriable("id") Interge id){
}