Spring Framework RestTemplate - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework RestTemplate class.

This code example shows how to use the following methods:setMessageConverters, setRequestFactory
	public void setup() { 
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); 
		converters.add(new StringHttpMessageConverter()); 
		converters.add(new MappingJackson2HttpMessageConverter()); 
 
		this.restTemplate = new RestTemplate(); 
		this.restTemplate.setMessageConverters(converters); 
 
		this.mockServer = MockRestServiceServer.createServer(this.restTemplate); 
	} 
 
	@Test 
	public void testString() throws Exception { 
 
		this.mockServer.expect(requestTo("/person/1")) 
			.andExpect(header("Accept", "application/json, application/*+json")) 
			.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); 
 
		this.restTemplate.getForObject(new URI("/person/1"), Person.class); 
		this.mockServer.verify(); 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getRequestFactory
	private ClientHttpRequestFactory factory;
 
 
	@Before 
	public void setup() { 
		RestTemplate restTemplate = new RestTemplate();
		this.server = MockRestServiceServer.createServer(restTemplate);
		this.factory = restTemplate.getRequestFactory();
	} 
 
	@Test
	public void createRequest() throws Exception {
		URI uri = new URI("/foo");
		ClientHttpRequest expected = (ClientHttpRequest) this.server.expect(anything());
		ClientHttpRequest actual = this.factory.createRequest(uri, HttpMethod.GET);
 
		assertSame(expected, actual);
		assertEquals(uri, actual.getURI());
		assertEquals(HttpMethod.GET, actual.getMethod());
	} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setMessageConverters, setRequestFactory
	public void setup() { 
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); 
		converters.add(new StringHttpMessageConverter()); 
		converters.add(new MappingJackson2HttpMessageConverter()); 
 
		this.restTemplate = new RestTemplate(); 
		this.restTemplate.setMessageConverters(converters); 
 
		this.mockServer = MockRestServiceServer.createServer(this.restTemplate); 
	} 
 
	@Test 
	public void contentType() throws Exception { 
		this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); 
		this.restTemplate.put(new URI("/foo"), new Person()); 
		this.mockServer.verify(); 
	} 
 
	@Test 
	public void contentTypeNoMatch() throws Exception { 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:
 
	/** 
	 * Construct a new instance of the {@link RestGatewaySupport}, with default parameters. 
	 */ 
	public RestGatewaySupport() { 
		this.restTemplate = new RestTemplate();
	} 
 
	/** 
	 * Construct a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}. 
	 * @see RestTemplate#RestTemplate(ClientHttpRequestFactory) 
	 */ 
	public RestGatewaySupport(ClientHttpRequestFactory requestFactory) {
		Assert.notNull(requestFactory, "'requestFactory' must not be null");
		this.restTemplate = new RestTemplate(requestFactory);
	} 
 
 
	/** 
	 * Sets the {@link RestTemplate} for the gateway. 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setMessageConverters, setRequestFactory
		this.people = new PeopleWrapper(composers); 
 
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); 
		converters.add(new Jaxb2RootElementHttpMessageConverter()); 
 
		this.restTemplate = new RestTemplate(); 
		this.restTemplate.setMessageConverters(converters); 
 
		this.mockServer = MockRestServiceServer.createServer(this.restTemplate); 
	} 
 
	@Test 
	public void testXmlEqualTo() throws Exception { 
		this.mockServer.expect(requestTo("/composers")) 
			.andExpect(content().contentType("application/xml")) 
			.andExpect(content().xml(PEOPLE_XML)) 
			.andRespond(withSuccess()); 
 
		this.restTemplate.put(new URI("/composers"), this.people); 
		this.mockServer.verify(); 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getForObject, setMessageConverters
		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"); 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setMessageConverters, setRequestFactory
		this.people.add("performers", new Person("Yehudi Menuhin")); 
 
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); 
		converters.add(new MappingJackson2HttpMessageConverter()); 
 
		this.restTemplate = new RestTemplate(); 
		this.restTemplate.setMessageConverters(converters); 
 
		this.mockServer = MockRestServiceServer.createServer(this.restTemplate); 
	} 
 
	@Test 
	public void testExists() throws Exception { 
		this.mockServer.expect(requestTo("/composers")) 
			.andExpect(content().contentType("application/json;charset=UTF-8")) 
			.andExpect(jsonPath("$.composers[0]").exists()) 
			.andExpect(jsonPath("$.composers[1]").exists()) 
			.andExpect(jsonPath("$.composers[2]").exists()) 
			.andExpect(jsonPath("$.composers[3]").exists()) 
			.andRespond(withSuccess()); 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:
 */ 
public class UserJaxRsFT extends BaseFunctionalTestCase { 
 
	private static String resourceUrl = baseUrl + "/cxf/jaxrs/user";
 
	private RestTemplate restTemplate = new RestTemplate();
 
	@Test
	public void getUser() { 
		UserDTO user = restTemplate.getForObject(resourceUrl + "/{id}.xml", UserDTO.class, 1L);
		assertThat(user.getLoginName()).isEqualTo("admin");
		assertThat(user.getName()).isEqualTo("管理员");
		assertThat(user.getTeamId()).isEqualTo(1);
 
		try { 
			user = restTemplate.getForObject(resourceUrl + "/{id}.json", UserDTO.class, 1L);
		} catch (HttpStatusCodeException e) {
			fail(e.getMessage());
		} 
		assertThat(user.getLoginName()).isEqualTo("admin");
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setRequestFactory
 
	private RestTemplate restTemplate; 
 
	@Before 
	public void setup() { 
		this.restTemplate = new RestTemplate(); 
		this.mockServer = MockRestServiceServer.createServer(this.restTemplate); 
	} 
 
	@Test 
	public void performGet() throws Exception { 
 
		String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; 
 
		this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) 
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON)); 
 
		@SuppressWarnings("unused") 
		Person ludwig = restTemplate.getForObject("/composers/{id}", Person.class, 42); 
 

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setMessageConverters
	public static String URL =  "http://127.0.0.1:5984/spring_demo/";
	 
	 
	@Bean 
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
		 
		//converters.add(new MappingJacksonHttpMessageConverter()); 
		converters.add(new CouchDbMappingJacksonHttpMessageConverter());
		restTemplate.setMessageConverters(converters);
		return restTemplate;
	} 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
See Code Examples for Spring Framework RestTemplate Methods: