Skip to content

Instantly share code, notes, and snippets.

@areguig
Last active April 10, 2018 09:53
Show Gist options
  • Save areguig/24c90d5aad0f7ce3596fd5945b6e9601 to your computer and use it in GitHub Desktop.
Save areguig/24c90d5aad0f7ce3596fd5945b6e9601 to your computer and use it in GitHub Desktop.
package io.github.areguig.demo;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
@RestController
public class Controller {
@RequestMapping(value = "/api/in", method = RequestMethod.PUT, consumes = "application/json")
public ResponseEntity<?> updateProject(
@RequestBody ProjectMetadata metadata) throws JSONException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic <aut key>");
JSONObject requestBody = new JSONObject();
requestBody.put("label", metadata.getLabel());
requestBody.put("description", metadata.getDescription());
requestBody.put("shortDesc", metadata.getShortDesc());
requestBody.put("tags", metadata.getTags());
System.out.println("requestBody" + requestBody);
HttpEntity<JSONObject> requestEntity = new HttpEntity<>(requestBody, headers);
System.out.println("requestentity" + requestEntity.toString());
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/api/external";
System.out.println("trying to send request to create project");
ResponseEntity<?> createResponse = null;
try {
createResponse = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, String.class);
// restTemplate.put(url, requestEntity,projectKey);
} catch (HttpClientErrorException e) {
System.out.println("Exceptions occurred while processing the request::");
return new ResponseEntity<String>(e.getResponseBodyAsString(), HttpStatus.BAD_REQUEST);
}
return createResponse;
}
@RestController
public class ControllerExternal {
@PutMapping(value = "/api/external")
public String putExternal(@RequestBody ProjectMetadata projectProjectMetadata){
if(projectProjectMetadata.getTags()==null){
System.out.printf("no tags");
}
System.out.printf("EXTERNAL "+ JSONObject.fromObject(projectProjectMetadata).toString());
return JSONObject.fromObject(projectProjectMetadata).toString();
}
}
}
Copy link

ghost commented Apr 10, 2018

Hey arguig,thanks for working on my code but just fyi the request is forming correctly but the operation is failing @put

requestBody{"description":"adsfy","shortDesc":"nsfdf","label":"testapp","tag":["tag12"]}
2018-04-10 13:54:10,362 8682 [http-nio-9080-exec-10] DEBUG c.p.controller.ProjectController - requestentity<{"description":"adsfy","shortDesc":"nsfdf","label":"testapp","tag":["tag12"]},{Content-Type=[application/json], Authorization=[Basic aut]}>
2018-04-10 13:54:10,370 8690 [http-nio-9080-exec-10] DEBUG c.p.controller.ProjectController - trying to send request to dataiku to create project
2018-04-10 13:54:10,394 8714 [http-nio-9080-exec-10] DEBUG o.s.web.client.RestTemplate - Created PUT request for "http://<>/public/api/projects/abcde/metadata/"
2018-04-10 13:54:10,398 8718 [http-nio-9080-exec-10] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, /]
2018-04-10 13:54:10,398 8718 [http-nio-9080-exec-10] DEBUG o.s.web.client.RestTemplate - Writing [{"description":"adsfy","shortDesc":"nsfdf","label":"testapp","tag":["tag12"]}] as "application/json" using [org.springframework.http.converter.StringHttpMessageConverter@189c0555]
2018-04-10 13:54:10,954 9274 [http-nio-9080-exec-10] DEBUG o.s.web.client.RestTemplate - PUT request for "http://<>/public/api/projects/abcde/metadata/" resulted in 500 (Server Error); invoking error handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment