Первое приложение:
@RestController
@RequestMapping("/firstApp")
public class FirstAppController{
@Autowired
private StudentRepository studentRepository;
private RestTemplate restTemplate =new RestTemplate();
@PostMapping
public ResponseEntity<String> save(@RequestBody Student student){
Student savedStudent = studentRepository.saveAndFlush(student);
//set your headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//set your entity to send
HttpEntity entity = new HttpEntity(savedStudent ,headers);
// send it!
return restTemplate.exchange("http://localhost:8081/secondeApp", HttpMethod.POST, entity, String.class);
}
}
URL доступа первой службы: [POST]: http;//localhost:8080/firstApp
Второе приложение:
@RestController
@RequestMapping("/secondeApp")
public class SecondeAppController{
@Autowired
private StudentRepository studentRepository;
@PostMapping(consumes=MediaType.APPLICATION_JSON)
public void save(@RequestBody Student student){
studentRepository.saveAndFlush(student);
}
}