Search in sources :

Example 31 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-data-document-examples by spring-projects.

the class RestTemplateActivity method onCreate.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView view = new TextView(this);
    view.append("Running tests...\n\n");
    setContentView(view);
    try {
        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(new MappingJacksonHttpMessageConverter());
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setMessageConverters(converters);
        Map response = restTemplate.getForObject("http://127.0.0.1:5984", Map.class);
        view.append(response.toString());
        /*
			URL url = new URL("http:/127.0.0.1:5984");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			List<String> lines= new ArrayList<String>();
			while((line = reader.readLine()) != null){
			    lines.add(line);
			}
			reader.close();
			view.append(lines.toString());*/
        view.append("all ok!");
    } catch (Exception e) {
        view.append(e.toString());
        Log.e("rest-template", "this is bad", e);
    }
}
Also used : MappingJacksonHttpMessageConverter(org.springframework.http.converter.json.MappingJacksonHttpMessageConverter) ArrayList(java.util.ArrayList) MappingJacksonHttpMessageConverter(org.springframework.http.converter.json.MappingJacksonHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) TextView(android.widget.TextView) Map(java.util.Map)

Example 32 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-framework by spring-projects.

the class RequestPartIntegrationTests method setup.

@Before
public void setup() {
    ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
    emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
    converters.add(emptyBodyConverter);
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new ResourceHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter());
    AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
    converter.setPartConverters(converters);
    restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    restTemplate.setMessageConverters(Collections.singletonList(converter));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Before(org.junit.Before)

Example 33 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-boot-admin by codecentric.

the class ApplicationRegistratorTest method setup.

@Before
public void setup() {
    restTemplate = mock(RestTemplate.class);
    adminProps = new AdminProperties();
    adminProps.setUrl(new String[] { "http://sba:8080", "http://sba2:8080" });
    ApplicationFactory factory = mock(ApplicationFactory.class);
    when(factory.createApplication()).thenReturn(Application.create("AppName").withManagementUrl("http://localhost:8080/mgmt").withHealthUrl("http://localhost:8080/health").withServiceUrl("http://localhost:8080").build());
    registrator = new ApplicationRegistrator(restTemplate, adminProps, factory);
    headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
}
Also used : AdminProperties(de.codecentric.boot.admin.client.config.AdminProperties) HttpHeaders(org.springframework.http.HttpHeaders) ApplicationRegistrator(de.codecentric.boot.admin.client.registration.ApplicationRegistrator) RestTemplate(org.springframework.web.client.RestTemplate) ApplicationFactory(de.codecentric.boot.admin.client.registration.ApplicationFactory) Before(org.junit.Before)

Example 34 with RestTemplate

use of org.springframework.web.client.RestTemplate in project goci by EBISPOT.

the class EnsemblRestTemplateService method exec.

public RestResponseResult exec(String url) {
    ResponseEntity<String> out;
    RestResponseResult result = new RestResponseResult();
    RestTemplate restTemplate = this.getRestTemplate();
    HttpEntity<Object> entity = this.getEntity();
    getLog().debug("Querying " + url);
    //and do I need this JSON media type for my use case?
    try {
        out = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        result.setStatus(out.getStatusCode().value());
        result.setUrl(url);
        JsonNode body = new JsonNode(out.getBody().toString());
        result.setRestResult(body);
        getLog().debug("Response: 200");
    //result.setValue(out.getBody());
    } catch (EnsemblRestClientException erce) {
        getLog().debug("EnsemblRestClientException");
        result = erce.getEnsemblLookup();
        result.setUrl(url);
    } catch (Exception e) {
        getLog().debug("Exception not managed");
    }
    return result;
}
Also used : EnsemblRestClientException(uk.ac.ebi.spot.goci.exception.EnsemblRestClientException) RestTemplate(org.springframework.web.client.RestTemplate) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JSONObject(org.json.JSONObject) JsonNode(com.mashape.unirest.http.JsonNode) MalformedURLException(java.net.MalformedURLException) EnsemblRestClientException(uk.ac.ebi.spot.goci.exception.EnsemblRestClientException)

Example 35 with RestTemplate

use of org.springframework.web.client.RestTemplate in project goci by EBISPOT.

the class EnsemblRestTemplateService method getRestTemplate.

public RestTemplate getRestTemplate() {
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) CustomResponseErrorHandler(uk.ac.ebi.spot.goci.exception.CustomResponseErrorHandler) RestTemplate(org.springframework.web.client.RestTemplate)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)206 Test (org.junit.Test)107 URI (java.net.URI)39 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)29 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)22 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)22 HttpHeaders (org.springframework.http.HttpHeaders)19 HttpEntity (org.springframework.http.HttpEntity)16 MapPropertySource (org.springframework.core.env.MapPropertySource)15 StandardEnvironment (org.springframework.core.env.StandardEnvironment)15 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)15 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)15 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)13 IOException (java.io.IOException)12 JSONObject (org.json.JSONObject)12 Before (org.junit.Before)11 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)11 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)11