Posts

Showing posts with the label JSON

send request with body - RestTemplate

HttpHeaders headers = new HttpHeaders();  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();  data.add("client_id",clientId);  data.add("client_secret",clientSecret);  data.add("grant_type",grantType);  data.add("redirect_uri",redirectUrl);  data.add("code",code.split("#")[0]);  HttpEntity<MultiValueMap<String, String>>  request = new HttpEntity<>(data, headers);  ResponseEntity<String> response = restTemplate.postForEntity(accessTokenUrl,request,String.class);

json to object - object to json methods

import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; public class JsonHelper {     public static String getJsonString(Object object) throws JsonProcessingException {         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);         return mapper.writeValueAsString(object);     }     public static <T> T getObjectFromJson(String json, Class<T> type) throws JsonProcessingException{         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFe...