繁体   English   中英

如何在springboot中使用rest模板保存新对象

[英]how I can use a rest template in springboot to save new Object

我有两个拖曳应用程序的学生,并使用两个拖曳端口80808081 在第一个应用程序中,我有一张桌子,并且有一种保存学生的方法。 但是问题是如何从此方法发送此已保存的对象到其他应用程序,并将其保存在第二个应用程序的表中。 一个以上的应用程序之间有休息吗?

第一个应用程序中的方法:

saveStudent() ... StudentDao.save();

secont应用程序中的方法:

save Income() ... 

第一个应用程序:

@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);
     }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM