The spring boot environment is not built much, and the code is direct
To configure
Add the following configuration to the startup class
@Autowired private RestTemplateBuilder builder; // Use RestTemplateBuilder to instantiate the RestTemplate object. spring has injected RestTemplateBuilder by default @Bean public RestTemplate restTemplate() { return builder.build(); }
Next, RestTemplate is injected into the controller class
@Autowired private RestTemplate restTemplate;
Code testing
Let's take a look at the entity classes used for testing
@Data @Component public class Device { private Integer id; private String type; private String manufactor; //Manufacturer @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss+0800",timezone = "GMT+8:00") private Date registerTime; }
The following is a detailed call for adding, deleting, modifying and querying. The previous path variable is that I set a rest path, which is modified by myself
/* Query a device information */ @GetMapping("/getDevice/{id}") public String getDevice(@PathVariable int id){ String forObject = restTemplate.getForObject(path+"/device/"+ id +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn", String.class); return forObject; } /* http://127.0.0.1:8181/cxf/device?at=Jshb9jiOa4hi240POyOP58g4OHHBQ2sF Add device post request */ @PostMapping("/addDevice") public String addDevice(@Valid Device device){ Device body = restTemplate.postForEntity(path+"/device?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn", device, Device.class).getBody(); return body.toString(); } /* modify */ @PutMapping("/updateDevice") public String updateDevice(@Valid Device device){ restTemplate.put(path+"/cxf/device/"+ device.getId() +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn",device); return "Modified success"; } /* delete */ @DeleteMapping("/delDevice/{id}") public String delDevice(@PathVariable int id){ restTemplate.delete(path+"/device/"+ id +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn"); return "Delete successful"; }
Configuration of postman request parameters
LZ uses the postman test. By the way, paste the parameter settings of post request and put request. There are many forms. I just chose one randomly
put request
post request
The headers settings are the same for LZ2 requests