com.springsource.samples.resttemplate.ClientRest.java Source code

Java tutorial

Introduction

Here is the source code for com.springsource.samples.resttemplate.ClientRest.java

Source

/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.springsource.samples.resttemplate;

import java.net.URI;
import java.util.List;

import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class ClientRest {

    private static RestTemplate restTemplate = new RestTemplate();

    public static void main(String[] args) {

        ClientRest clientRest = new ClientRest();

        clientRest.testaggiungiCommento();
        //      clientRest.testStringParameterOk();

    }

    public void testaggiungiCommento() {

        //      MyMessageConverter<CommentDTO> myMessageConverter = new MyMessageConverter<CommentDTO>();
        //      
        //      List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        //      messageConverters.add(myMessageConverter);
        //      StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        //      messageConverters.add(stringHttpMessageConverter);
        //      restTemplate.setMessageConverters(messageConverters);

        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

        for (HttpMessageConverter<?> httpMessageConverter : messageConverters) {

            System.out.println(httpMessageConverter.getClass());
        }

        restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setText("aaa");
        commentDTO.setValore(4);
        //      CommentDTO postForObject = restTemplate.postForObject("http://localhost:8080/rest-validation/api/aggiungiCommento",
        //            commentDTO, CommentDTO.class);

        URI postForLocation = restTemplate
                .postForLocation("http://localhost:8080/rest-validation/api/aggiungiCommento", commentDTO);
        //            
        //      System.out.println(postForLocation.getQuery());
    }

    public void testIntParameterNull() {
        String result = restTemplate.getForObject("http://localhost:8080/rest-validation/api/parametroInt?valore=",
                String.class);
        System.out.println(result);
    }

    public void testIntParameterKoMax10() {
        String result = restTemplate.getForObject(
                "http://localhost:8080/rest-validation/api/parametroInt?valore=11121212121", String.class);
        System.out.println(result);
    }

    public void testIntParameterOk() {
        String result = restTemplate.getForObject("http://localhost:8080/rest-validation/api/parametroInt?valore=5",
                String.class);
        System.out.println(result);
    }

    public void testStringParameterOk() {
        String result = restTemplate.getForObject("http://localhost:8080/rest-validation/api/ciao?valore=sdsd",
                String.class);
        System.out.println(result);
    }

    public void testStringParameterNullParameter() {
        String result = restTemplate.getForObject("http://localhost:8080/rest-validation/api/ciao", String.class);
        System.out.println(result);
    }

    public void testStringParameterEmptyParameter() {
        String result = restTemplate.getForObject("http://localhost:8080/rest-validation/api/ciao?valore=",
                String.class);
        System.out.println(result);
    }

}