前言:
一直使用HttpClient做服务器调用 但是老是接收参数有问题,今天把它的常见的方法,用CRUD写了一遍 希望对你有用
1. POST©
send
- postForEntity(url, request就是需要保存的对象,responseType响应结果类型)
- postForObject(url, request就是需要保存的对象,responseType响应结果类型)
@PostMapping("/saveproduct")
public ResponseEntity<String> saveProduct(Product product){
String url = "http://localhost:8081/product/save";
// 使用对象接收crm系统的返回
ResponseEntity<String> entity = restTemplate.postForEntity(url,product,String.class);
return entity ;
}
get 201
@PostMapping("/save")
public ResponseEntity<String> saveProduct(@RequestBody Product product){
//save
productService.saveProduct(product);
//201
return new ResponseEntity<>(HttpStatus.CREATED);
}
2. GET ®
send
- getForEntity(url, responseType响应结果类型) -返回响应体+响应状态码+等等
- getForObject(url, responseType响应结果类型) -返回响应体,会自动封装成指定的类型
- 如果不需要返回值,responseType=Void.class
@GetMapping("/mailclassrid")
public ResponseEntity<String> findCategoryByMainClassrId(String mid){
String url = "http://localhost:8081/category/findCategoryByMainClassrId?mid="+mid;
ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
return entity ;
}
get 200
@GetMapping("/findCategoryByMainClassrId")
public ResponseEntity<List<Category>> findCategoryByMid(String mid){
if(mid==null){
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
List<Category> categoryByMid = categoryService.findCategoryByMid(Integer.parseInt(mid));
return new ResponseEntity<>(categoryByMid,HttpStatus.OK);
}
3. PUT(U)
send
- put(url, request就是需要保存的对象)
@PutMapping("/updateproduct")
public ResponseEntity<String> findProductByPid(Product product){
String url = "http://localhost:8081/product/updateproduct";
// 使用对象接收crm系统的返回
restTemplate.put(url,product);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
get 204
@PutMapping("/updateproduct")
public void findProductByPid(@RequestBody Product product){
if (product!=null){
productService.updateProduct(product);
}
}
4. DELETE(D)
send
- delete(url)
@DeleteMapping("/deletes")
public void delete(String ids){
String url = "http://localhost:8081/product/"+ids;
try {
restTemplate.delete(url);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(ids);
}
get 204
@DeleteMapping("/{ids}")
public ResponseEntity<Void> deleteCourier(@PathVariable("ids") String ids){
try {
// System.out.println(ids);
productService.deleteCourier(ids);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
如果上述方法传参还是传不过去则使用RequestEntity传参方法
public void hah(Product product){
String url="http://localhost:8081/product/";
//使用exchange执行post请求 传入参数 发送方式 访问路径
// HttpMethod.PUT/DELETE/POST/GET
RequestEntity<Product> requestEntity = new RequestEntity<>(product, HttpMethod.POST, URI.create(url));
ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class);
}
响应结果泛型处理ParameterizedTypeReference (指定返回值类型)
public void hah(Product product){
String url="http://localhost:8081/product/";
// 响应结果泛型处理:ParameterizedTypeReference
RequestEntity<Void> requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create(url));
//可以指定返回值类型 请求体 返回值类型
ResponseEntity<List<Product>> responseEntity = restTemplate.exchange(requestEntity,
new ParameterizedTypeReference<List<Product>>() {
});
}
祝你幸福
送你一首歌《我好像在哪见过你》 薛之谦附图1024程序员节 10-24-2018