resttemplate usage sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<artifactId>sample-resttemplate</artifactId> | |
<groupId>com.github.eeichinger.blogs</groupId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<!-- global build settings --> | |
<java.version>1.8</java.version> | |
<file.encoding>utf-8</file.encoding> | |
<project.build.sourceEncoding>${file.encoding}</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>${file.encoding}</project.reporting.outputEncoding> | |
<maven.compiler.source>${java.version}</maven.compiler.source> | |
<maven.compiler.target>${java.version}</maven.compiler.target> | |
<!-- dependency versions --> | |
<wiremock.version>1.58</wiremock.version> | |
<jackson.version>2.6.5</jackson.version> | |
<spring.version>4.2.5.RELEASE</spring.version> | |
<spring.boot.version>1.3.3.RELEASE</spring.boot.version> | |
<spring-restdocs.version>1.0.1.RELEASE</spring-restdocs.version> | |
<lombok.version>1.16.8</lombok.version> | |
<slf4j.version>1.7.21</slf4j.version> | |
<logback.version>1.1.6</logback.version> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-framework-bom</artifactId> | |
<version>${spring.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<!-- lombok for convenience, see https://projectlombok.org/ --> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>${lombok.version}</version> | |
<scope>provided</scope> | |
</dependency> | |
<!-- use slf4j and redirect all other libs to it --> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>${slf4j.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>${logback.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>jcl-over-slf4j</artifactId> | |
<version>${slf4j.version}</version> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>log4j-over-slf4j</artifactId> | |
<version>${slf4j.version}</version> | |
<scope>runtime</scope> | |
</dependency> | |
<!-- spring resttemplate deps --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>${spring.version}</version> | |
<exclusions> | |
<exclusion> | |
<groupId>commons-logging</groupId> | |
<artifactId>commons-logging</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<!-- json message binding --> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>${jackson.version}</version> | |
</dependency> | |
<!-- xml message binding --> | |
<dependency> | |
<groupId>com.fasterxml.jackson.dataformat</groupId> | |
<artifactId>jackson-dataformat-xml</artifactId> | |
<version>${jackson.version}</version> | |
</dependency> | |
<!-- test deps --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.github.tomakehurst</groupId> | |
<artifactId>wiremock</artifactId> | |
<version>${wiremock.version}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.eeichinger.resttemplate_sample; | |
import com.github.tomakehurst.wiremock.client.WireMock; | |
import com.github.tomakehurst.wiremock.junit.WireMockRule; | |
import lombok.Data; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.springframework.web.client.RestTemplate; | |
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | |
import static org.hamcrest.CoreMatchers.startsWith; | |
import static org.junit.Assert.assertThat; | |
/** | |
* @author Erich Eichinger | |
* @since 05/05/2016 | |
*/ | |
public class XmlRestTemplateTest { | |
@Rule | |
public WireMockRule wireMockRule = new WireMockRule(0); | |
@Test | |
public void fetchQuoteXml_should_pickup_correct_xmlmessageconverter_based_on_response_contenttype() { | |
stubFor(WireMock.get(WireMock.urlMatching("/sample/fetchxml")) | |
.willReturn(WireMock | |
.aResponse() | |
.withStatus(200) | |
.withHeader("Content-Type", "text/xml") | |
.withBody("" + | |
"<quote>\n" + | |
" <type>success</type>\n" + | |
" <value>\n" + | |
" <id>8</id>\n" + | |
" <quote>Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live</quote>\n" + | |
" </value>\n" + | |
"</quote>\n" | |
) | |
)); | |
RestTemplate restTemplate = new RestTemplate(); | |
Quote quote = restTemplate.getForObject("http://localhost:" + wireMockRule.port() + "/sample/fetchxml", Quote.class); | |
assertThat(quote.getValue().getQuote(), startsWith("Always code as if the guy")); | |
} | |
@Test | |
public void fetchQuoteJson_should_pickup_correct_jsonmessageconverter_based_on_response_contenttype() { | |
stubFor(WireMock.get(WireMock.urlMatching("/sample/fetchjson")) | |
.willReturn(WireMock | |
.aResponse() | |
.withStatus(200) | |
.withHeader("Content-Type", "application/json") | |
.withBody(" {\n" + | |
" \"type\": \"success\",\n" + | |
" \"value\": {\n" + | |
" \"id\": 8,\n" + | |
" \"quote\": \"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live\"\n" + | |
" }\n" + | |
" }\n" | |
) | |
)); | |
RestTemplate restTemplate = new RestTemplate(); | |
Quote quote = restTemplate.getForObject("http://localhost:" + wireMockRule.port() + "/sample/fetchjson", Quote.class); | |
assertThat(quote.getValue().getQuote(), startsWith("Always code as if the guy")); | |
} | |
@Data | |
public static class Quote { | |
String type; | |
Value value; | |
@Data | |
public static class Value { | |
Long id; | |
String quote; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment