searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Java中restTemplate的使用

2023-06-12 08:28:24
1
0

GitHub项目地址

Gitee项目地址

本文介绍restTemplate基础用法。

Java中get和post的用法请参考:Java中Get和Post的使用

1 提供get/post接口

1.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {

   @Autowired
   MyService myService;

   // 提供get接口
   @GetMapping("/provideGet")
   public Map<String, String> provideGet(){
       return myService.provideGet();
  }

   // 提供post接口
   @PostMapping("/providePost")
   public Map<String, Object> providePost(@RequestParam("number") int number, @RequestParam("name") String name) {
       return myService.providePost(number, name);
  }

   // 提供map参数的post接口
   @PostMapping("/providePostByMap")
   public Map<String, Object> providePostByMap(@RequestParam Map<String, Object> map) {
       return myService.providePostByMap(map);
  }

   // 调用get接口
   @GetMapping("/useGet")
   public Map<String, Object> useGet() {
       return myService.useGet();
  }
}

1.2 Service

@Service
@EnableScheduling
public class MyService {

   public Map<String, String> provideGet() {
       Map<String, String> res = new HashMap<>();
       res.put("number", "3");
       res.put("name", "张三get");
       System.out.println("provideGet res:" + res + "\n");
       return res;
  }

   public Map<String, Object> providePost(int number, String name) {
       Map<String, Object> res = new HashMap<>();
       res.put("number", number);
       res.put("name", name);

       return res;
  }

   public Map<String, Object> providePostByMap(Map<String, Object> map) {
       int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));
       String name = map.get("name") == null ? "" : (String) map.get("name");
       Map<String, Object> res = new HashMap<>();
       res.put("number", number);
       res.put("name", name);

       System.out.println("providePostByMap res:" + res + "\n");
       return res;
  }
}

 

2 调用get/post接口

使用restTemplate调用get/post接口。

  • getForObject():返回值是HTTP协议的响应体

  • getForEntity():返回的是ResponseEntityResponseEntity是对HTTP响应的封装,除了包含响应体,还包含HTTP状态码、contentType、contentLength、Header等信息

2.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {    
@Autowired
   MyService myService;

   // 调用get接口
   @GetMapping("/useGet")
   public Map<String, Object> useGet() {
       return myService.useGet();
  }

   // 调用get接口验证账号密码
   @GetMapping("/useGetByPsw")
   public Map<String, Object> useGetByPsw() {
       return myService.useGetByPsw();
  }

   // 调用post接口
   @PostMapping("/usePost")
   public Map<String, Object> usePost() {
       return myService.usePost();
  }
}

2.2 Service

@Service
@EnableScheduling
public class MyService {
   @Resource
   private RestTemplate restTemplate;

   String getURL = "http://localhost:8081/homepage/provideGet";
   String postURL = "http://localhost:8081/homepage/providePostByMap";

   public Map<String, Object> useGet() {
       // getForObject返回值是HTTP协议的响应体
       String strObject1 = restTemplate.getForObject(getURL, String.class); //无参
       JSONObject jsonObject1 = JSONObject.parseObject(strObject1);

       MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
       sendData.add("number", "3");
       sendData.add("name", "张三post");
       String strObject2 = restTemplate.getForObject(getURL, String.class, sendData); // 带参
       JSONObject jsonObject2 = JSONObject.parseObject(strObject2);

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("useGet jsonObject1:" + jsonObject1 + "\n");
       System.out.println("useGet jsonObject2:" + jsonObject2 + "\n");
       System.out.println("useGet responseData:" + responseData + "\n");
       System.out.println("useGet returnData:" + returnData + "\n");
       return returnData;
  }

   public Map<String, Object> useGetByPsw() {

       RestTemplateBuilder builder = new RestTemplateBuilder();
       RestTemplate restTemplate = builder.basicAuthentication("username", "password").build();

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("useGetByPsw returnData:" + responseData + "\n");
       System.out.println("useGetByPsw returnData:" + returnData + "\n");
       return returnData;
  }

   public Map<String, Object> usePost() {
       //RestTemplate在postForObject时,用MultiValueMap,不可使用HashMap。
       MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
       sendData.add("number", "3");
       sendData.add("name", "张三post");

       // getForObject返回值是HTTP协议的响应体
       String strObject = restTemplate.postForObject(postURL, sendData, String.class);
       JSONObject jsonObject = JSONObject.parseObject(strObject);

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(postURL, sendData, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("usePost jsonObject:" + jsonObject + "\n");
       System.out.println("usePost responseData:" + responseData + "\n");
       System.out.println("usePost returnData:" + returnData + "\n");
       return returnData;
  }
}

 

 

 

 

 

0条评论
0 / 1000
代码的路
100文章数
1粉丝数
代码的路
100 文章 | 1 粉丝
代码的路
100文章数
1粉丝数
代码的路
100 文章 | 1 粉丝
原创

Java中restTemplate的使用

2023-06-12 08:28:24
1
0

GitHub项目地址

Gitee项目地址

本文介绍restTemplate基础用法。

Java中get和post的用法请参考:Java中Get和Post的使用

1 提供get/post接口

1.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {

   @Autowired
   MyService myService;

   // 提供get接口
   @GetMapping("/provideGet")
   public Map<String, String> provideGet(){
       return myService.provideGet();
  }

   // 提供post接口
   @PostMapping("/providePost")
   public Map<String, Object> providePost(@RequestParam("number") int number, @RequestParam("name") String name) {
       return myService.providePost(number, name);
  }

   // 提供map参数的post接口
   @PostMapping("/providePostByMap")
   public Map<String, Object> providePostByMap(@RequestParam Map<String, Object> map) {
       return myService.providePostByMap(map);
  }

   // 调用get接口
   @GetMapping("/useGet")
   public Map<String, Object> useGet() {
       return myService.useGet();
  }
}

1.2 Service

@Service
@EnableScheduling
public class MyService {

   public Map<String, String> provideGet() {
       Map<String, String> res = new HashMap<>();
       res.put("number", "3");
       res.put("name", "张三get");
       System.out.println("provideGet res:" + res + "\n");
       return res;
  }

   public Map<String, Object> providePost(int number, String name) {
       Map<String, Object> res = new HashMap<>();
       res.put("number", number);
       res.put("name", name);

       return res;
  }

   public Map<String, Object> providePostByMap(Map<String, Object> map) {
       int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));
       String name = map.get("name") == null ? "" : (String) map.get("name");
       Map<String, Object> res = new HashMap<>();
       res.put("number", number);
       res.put("name", name);

       System.out.println("providePostByMap res:" + res + "\n");
       return res;
  }
}

 

2 调用get/post接口

使用restTemplate调用get/post接口。

  • getForObject():返回值是HTTP协议的响应体

  • getForEntity():返回的是ResponseEntityResponseEntity是对HTTP响应的封装,除了包含响应体,还包含HTTP状态码、contentType、contentLength、Header等信息

2.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {    
@Autowired
   MyService myService;

   // 调用get接口
   @GetMapping("/useGet")
   public Map<String, Object> useGet() {
       return myService.useGet();
  }

   // 调用get接口验证账号密码
   @GetMapping("/useGetByPsw")
   public Map<String, Object> useGetByPsw() {
       return myService.useGetByPsw();
  }

   // 调用post接口
   @PostMapping("/usePost")
   public Map<String, Object> usePost() {
       return myService.usePost();
  }
}

2.2 Service

@Service
@EnableScheduling
public class MyService {
   @Resource
   private RestTemplate restTemplate;

   String getURL = "http://localhost:8081/homepage/provideGet";
   String postURL = "http://localhost:8081/homepage/providePostByMap";

   public Map<String, Object> useGet() {
       // getForObject返回值是HTTP协议的响应体
       String strObject1 = restTemplate.getForObject(getURL, String.class); //无参
       JSONObject jsonObject1 = JSONObject.parseObject(strObject1);

       MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
       sendData.add("number", "3");
       sendData.add("name", "张三post");
       String strObject2 = restTemplate.getForObject(getURL, String.class, sendData); // 带参
       JSONObject jsonObject2 = JSONObject.parseObject(strObject2);

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("useGet jsonObject1:" + jsonObject1 + "\n");
       System.out.println("useGet jsonObject2:" + jsonObject2 + "\n");
       System.out.println("useGet responseData:" + responseData + "\n");
       System.out.println("useGet returnData:" + returnData + "\n");
       return returnData;
  }

   public Map<String, Object> useGetByPsw() {

       RestTemplateBuilder builder = new RestTemplateBuilder();
       RestTemplate restTemplate = builder.basicAuthentication("username", "password").build();

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("useGetByPsw returnData:" + responseData + "\n");
       System.out.println("useGetByPsw returnData:" + returnData + "\n");
       return returnData;
  }

   public Map<String, Object> usePost() {
       //RestTemplate在postForObject时,用MultiValueMap,不可使用HashMap。
       MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
       sendData.add("number", "3");
       sendData.add("name", "张三post");

       // getForObject返回值是HTTP协议的响应体
       String strObject = restTemplate.postForObject(postURL, sendData, String.class);
       JSONObject jsonObject = JSONObject.parseObject(strObject);

       // getForEntity返回的是ResponseEntity,是对HTTP响应的封装
       ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(postURL, sendData, ResponseResult.class);
       Map<String, Object> returnData = new HashMap<>();
       returnData.put("StatusCode:", responseData.getStatusCode());
       returnData.put("Body:", responseData.getBody());

       System.out.println("usePost jsonObject:" + jsonObject + "\n");
       System.out.println("usePost responseData:" + responseData + "\n");
       System.out.println("usePost returnData:" + returnData + "\n");
       return returnData;
  }
}

 

 

 

 

 

文章来自个人专栏
Java开发
31 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0