Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Created October 19, 2016 13:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save m-x-k/1530eb5090ec6dd8dcfec7aad5501b1d to your computer and use it in GitHub Desktop.
Save m-x-k/1530eb5090ec6dd8dcfec7aad5501b1d to your computer and use it in GitHub Desktop.
Post Jackson Object Via Rest Template
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