savantd

create customer with RestTemplate 1

Jun 23rd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. public class CustomerDTO {
  2.      private String name;
  3.      private String email;
  4.    
  5.      public CustomerDTO(String name, String email) {
  6.       this.name = name;
  7.       this.email = email;
  8.      }
  9.      public void setName(String name) {
  10.       this.name = name;
  11.      }
  12.      public String getName() {
  13.       return name;
  14.      }
  15.      public void setEmail(String email) {
  16.       this.email = email;
  17.      }
  18.      public String getEmail() {
  19.       return email;
  20.      }
  21. }
  22.    
  23. public class RestTemplateDemo {
  24.          public static void main(String args[]) {
  25.           RestTemplate restTemplate = new RestTemplate();
  26.           String customerAPIUrl = "http://localhost:9080/api/customer";
  27.           HttpEntity <CustomerDTO> request = new HttpEntity <>(new CustomerDTO("ABC", "[email protected]"));
  28.           ResponseEntity <CustomerDTO> customerEntity = restTemplate.exchange(customerAPIUrl, HttpMethod.POST, request, CustomerDTO.class);
  29.           System.out.println("Name:" + customerEntity.getBody().getName());
  30.           System.out.println("Email:" + customerEntity.getBody().getEmail());
  31.          }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment