Created
October 19, 2016 13:44
-
-
Save m-x-k/1530eb5090ec6dd8dcfec7aad5501b1d to your computer and use it in GitHub Desktop.
Post Jackson Object Via Rest Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.springframework.http.*; | |
import org.springframework.web.client.RestTemplate; | |
class PostJacksonObjectViaRestTemplate { | |
public static void main(String[] args) throws Exception { | |
String url = "http://localhost:8080/example/"; | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_JSON); | |
RestTemplate restTemplate = new RestTemplate(); | |
MyExample myExample = new MyExample("X"); | |
ObjectMapper mapper = new ObjectMapper(); | |
HttpEntity<String> request = new HttpEntity<>(mapper.writeValueAsString(myExample), headers); | |
String response = restTemplate.postForObject(url, request, String.class); | |
System.out.println(response); | |
} | |
} | |
class MyExample { | |
private String name; | |
public MyExample(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment