Example usage for org.springframework.web.client RestTemplate postForObject

List of usage examples for org.springframework.web.client RestTemplate postForObject

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate postForObject.

Prototype

@Override
    @Nullable
    public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,
            Map<String, ?> uriVariables) throws RestClientException 

Source Link

Usage

From source file:org.apache.servicecomb.demo.springmvc.client.SpringmvcClient.java

private static void testRequiredBody(RestTemplate template, String microserviceName) {
    String prefix = "cse://" + microserviceName;
    Person user = new Person();

    TestMgr.check("No user data found", template
            .postForObject(prefix + "/annotations/saysomething?prefix={prefix}", user, String.class, "ha"));

    user.setName("world");
    TestMgr.check("ha world", template.postForObject(prefix + "/annotations/saysomething?prefix={prefix}", user,
            String.class, "ha"));

    TestMgr.check("No user data found", template
            .postForObject(prefix + "/annotations/saysomething?prefix={prefix}", null, String.class, "ha"));

    TestMgr.check("No user name found",
            template.postForObject(prefix + "/annotations/say", "", String.class, "ha"));
    TestMgr.check("test", template.postForObject(prefix + "/annotations/say", "test", String.class, "ha"));
}

From source file:org.apache.servicecomb.demo.springmvc.client.SpringmvcClient.java

private static void testController(RestTemplate template, String microserviceName) {
    String prefix = "cse://" + microserviceName;

    TestMgr.check(7, template.getForObject(prefix + "/controller/add?a=3&b=4", Integer.class));

    try {/*from ww w. j a  v a 2s  .  c om*/
        template.getForObject(prefix + "/controller/add", Integer.class);
        TestMgr.check("failed", "success");
    } catch (InvocationException e) {
        TestMgr.check(e.getStatusCode(), 400);
    }

    TestMgr.check("hi world [world]",
            template.getForObject(prefix + "/controller/sayhi?name=world", String.class));

    TestMgr.check("hi world1 [world1]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, "world1"));
    TestMgr.check("hi hi  [hi ]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, "hi "));

    Map<String, String> params = new HashMap<>();
    params.put("name", "world2");
    TestMgr.check("hi world2 [world2]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, params));

    TestMgr.check("hello world",
            template.postForObject(prefix + "/controller/sayhello/{name}", null, String.class, "world"));
    TestMgr.check("hello hello ",
            template.postForObject(prefix + "/controller/sayhello/{name}", null, String.class, "hello "));

    HttpHeaders headers = new HttpHeaders();
    headers.add("name", "world");
    @SuppressWarnings("rawtypes")
    HttpEntity entity = new HttpEntity<>(null, headers);
    ResponseEntity<String> response = template.exchange(prefix + "/controller/sayhei", HttpMethod.GET, entity,
            String.class);
    TestMgr.check("hei world", response.getBody());

    Person user = new Person();
    user.setName("world");
    TestMgr.check("ha world", template.postForObject(prefix + "/controller/saysomething?prefix={prefix}", user,
            String.class, "ha"));
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Create a new job on Crowdflower using supplied job class. If this class had been retrieved with retrieveJob(String jobid),
 * then you copied the job without its data. Can be used to copy jobs across different accounts if API key is changed between the call to retrieveJob
 * @param job - job class that should be replicated as new job on Crowdflower
 * @return job class which represents the new job
 * @throws HttpServerErrorException/* w  w  w  .j  a v  a 2  s.c o  m*/
 */
CrowdJob createNewJob(CrowdJob job) throws HttpServerErrorException {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    JsonNode result = restTemplate.postForObject(newJobURL, job.getArgumentMap(), JsonNode.class, apiKey);

    return new CrowdJob(result);
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Upload the data vector as JSON to the specified Crowdflower job
 * @param job/* w w  w .j  a  va 2s  .  c om*/
 * @param data - Generic vector of data, containing ordinary java classes.
 * They should be annotated so that Jackson understands how to map them to JSON.
 *
 */

void uploadData(CrowdJob job, List<?> data) {
    Log LOG = LogFactory.getLog(getClass());

    //Crowdflower wants a Multi-line JSON, with each line having a new JSON object
    //Thus we have to map each (raw) object in data individually to a JSON string

    ObjectMapper mapper = new ObjectMapper();
    String jsonObjectCollection = "";

    StringBuilder jsonStringBuilder = new StringBuilder();
    int count = 0;
    for (Object obj : data) {
        count++;
        JsonNode jsonData = mapper.convertValue(obj, JsonNode.class);
        jsonStringBuilder.append(jsonData.toString());
        jsonStringBuilder.append("\n");
    }

    jsonObjectCollection = jsonStringBuilder.toString();

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> request = new HttpEntity<String>(jsonObjectCollection, headers);

    String result = "";

    if (job == null) {
        LOG.info("Upload new data and create new job: " + String.valueOf(count) + " data items");
        result = restTemplate.postForObject(uploadDataURL, request, String.class, apiKey);
    } else {
        LOG.info("Uploading new data to job: " + job.getId() + ": " + String.valueOf(count) + " data items");
        result = restTemplate.postForObject(uploadDataWithJobURL, request, String.class, job.getId(), apiKey);
    }
    LOG.info("Upload response:" + result);

    //set gold? this is what i would like to do...
    //updateVariable(job, "https://api.crowdflower.com/v1/jobs/{jobid}/gold?key={apiKey}", "set_standard_gold", "TRUE");
}

From source file:com.wisemapping.rest.MindmapController.java

private void saveMindMap(HttpServletRequest request, final Mindmap mindmap)
        throws UnsupportedEncodingException, IOException {
    RestTemplate restTemplate = new RestTemplate();
    MindMapVO mindMapVO = generateMindMapVO(request, mindmap);
    UserVO userVO = new UserVO();
    userVO.setId(mindmap.getIdUserConcept());
    mindMapVO.setUserCo(userVO);//from w w  w. ja  va 2s .  co  m
    Map<String, String> vars = new HashMap<String, String>();
    logger.debug("requestVO ->" + mindMapVO);
    restTemplate.postForObject(mindmapService.getUrlSave(), mindMapVO, ResponseVO.class, vars);
    // logger.debug("postForObject ->" + postForObject);
}