+++ /dev/null
-gradle eclipse
-gradle build
-java -jar build/libs/rest-example-0.0.1.jar
+++ /dev/null
-buildscript {
- repositories {
- mavenCentral()
- }
- dependencies {
- classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
- }
-}
-
-apply plugin: 'java'
-apply plugin: 'eclipse-wtp'
-apply plugin: 'spring-boot'
-
-jar {
- baseName = 'rest-example'
- version = '0.0.1'
-}
-
-repositories {
- mavenCentral()
-}
-
-sourceCompatibility = 1.8
-targetCompatibility = 1.8
-
-dependencies {
- compile("org.springframework.boot:spring-boot-starter-web")
- testCompile("junit:junit")
-}
-
-task wrapper(type: Wrapper) {
- gradleVersion = '2.3'
-}
--- /dev/null
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>de.spring.webservices</groupId>
+ <artifactId>web-services-spring-rest</artifactId>
+ <packaging>war</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>web-services-spring-rest</name>
+ <url>http://gumartinm.name</url>
+ <description>Web Services REST Spring Framework</description>
+ <organization>
+ <name>Gustavo Martin Morcuende</name>
+ <url>http://www.gumartinm.name</url>
+ </organization>
+ <scm>
+ <developerConnection>scm:git:http://git.gumartinm.name/JavaForFun</developerConnection>
+ <url>http://git.gumartinm.name/JavaForFun</url>
+ </scm>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <spring.version>4.2.4.RELEASE</spring.version>
+ </properties>
+
+ <profiles>
+ <profile>
+ <id>release</id>
+ <properties>
+ <environment.profile>release</environment.profile>
+ </properties>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ </profile>
+ </profiles>
+
+ <dependencies>
+ <!--
+ 1/3 Required dependency for log4j 2 with slf4j: binding between log4j 2 and slf4j
+ -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j-impl</artifactId>
+ <version>2.3</version>
+ </dependency>
+ <!--
+ 2/3 Required dependency for log4j 2 with slf4j: log4j 2 maven plugin (it is the log4j 2 implementation)
+ -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>2.3</version>
+ </dependency>
+ <!--
+ 3/3 Required dependency for getting rid of commons logging.
+ This is the BRIDGE (no binding) between Jakarta Commons Logging (used by Spring) and whatever
+ I am using for logging (in this case I am using log4j 2)
+ See: http://www.slf4j.org/legacy.html
+
+ We need exclusions in every dependency using Jakarta Commons Logging (see Spring dependencies below)
+ -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl-over-slf4j</artifactId>
+ <version>1.7.12</version>
+ </dependency>
+
+ <dependency>
+ <groupId>cglib</groupId>
+ <artifactId>cglib</artifactId>
+ <version>2.2.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>${spring.version}</version>
+ <!--
+ Required dependency for getting rid of commons logging and use my own logging library
+ (in my case I decided to use log4j 2 under slf4j)
+ -->
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-webmvc</artifactId>
+ <version>${spring.version}</version>
+ <!--
+ Required dependency for getting rid of commons logging and use my own logging library
+ (in my case I decided to use log4j 2 under slf4j)
+ -->
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <!-- Required by spring-webmvc -->
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <version>4.0.0-b01</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Jackson JSON Processor, required by spring-webmvc. See messageConverters in rest-config.xml -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.6.4</version>
+ </dependency>
+
+ <!-- Required by spring-context for using JSR-303. See LocalValidatorFactoryBean in rest-config.xml -->
+ <dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ <version>1.1.0.Final</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-validator</artifactId>
+ <version>5.2.2.Final</version>
+ </dependency>
+
+
+ <!-- Unitary and integration tests -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.12</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${spring.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.0.11-beta</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Required by MockMvcResultMatchers (spring-test framework) -->
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ <version>1.3</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-library</artifactId>
+ <version>1.3</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.jayway.jsonpath</groupId>
+ <artifactId>json-path</artifactId>
+ <version>2.1.0</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+ <build>
+ <finalName>${project.artifactId}</finalName>
+ <resources>
+ <resource>
+ <directory>${basedir}/src/main/webapp</directory>
+ <excludes>
+ <exclude>**/*.*</exclude>
+ </excludes>
+ </resource>
+ <resource>
+ <directory>${basedir}/src/main/resources/</directory>
+ <includes>
+ <include>**/*.*</include>
+ </includes>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.3</version>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ <encoding>${project.build.sourceEncoding}</encoding>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>2.7</version>
+ <configuration>
+ <encoding>${project.build.sourceEncoding}</encoding>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.4</version>
+ <configuration>
+ <archive>
+ <manifestEntries>
+ <Specification-Title>${project.description}</Specification-Title>
+ <Specification-Version>${project.version}</Specification-Version>
+ <Specification-Vendor>${project.organization.name}</Specification-Vendor>
+ <Implementation-Title>${project.description}</Implementation-Title>
+ <Implementation-Version>${project.version}</Implementation-Version>
+ <Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
+ </manifestEntries>
+ </archive>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.18.1</version>
+ <configuration>
+ <excludes>
+ <exclude>**/*IntegrationTest.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <version>2.18.1</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>integration-test</goal>
+ <goal>verify</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <includes>
+ <include>**/*IntegrationTest.java</include>
+ </includes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>2.6</version>
+ <configuration>
+ <webResources>
+ <resource>
+ <filtering>true</filtering>
+ <directory>src/main/webapp</directory>
+ <includes>
+ <include>WEB-INF/web.xml</include>
+ </includes>
+ </resource>
+ </webResources>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
--- /dev/null
+package de.spring.webservices.rest;
+
+public class Car {
+
+ private final Long id;
+ private final String content;
+
+ public Car(Long id, String content) {
+ this.id = id;
+ this.content = content;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getContent() {
+ return content;
+ }
+}
--- /dev/null
+package de.spring.webservices.rest;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/api/cars/")
+public class CarController {
+ private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
+ private static final String TEMPLATE = "Car: %s";
+
+ private final AtomicLong counter = new AtomicLong();
+
+ @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
+ @ResponseStatus(HttpStatus.OK)
+ public List<Car> cars() {
+ final List<Car> cars = new ArrayList<>();
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
+
+ return cars;
+ }
+
+ @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
+ @ResponseStatus(HttpStatus.OK)
+ public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
+ @PathVariable("id") long id,
+ @RequestParam Map<String, String> params,
+ @RequestParam(value = "wheel", required = false) String[] wheelParams) {
+
+ if (specialHeader != null) {
+ LOGGER.info("SPECIAL HEADER: " + specialHeader);
+ }
+
+ if (params.get("mirror") != null) {
+ LOGGER.info("MIRROR: " + params.get("mirror"));
+ }
+
+ if (params.get("window") != null) {
+ LOGGER.info("WINDOW: " + params.get("window"));
+ }
+
+ if (wheelParams != null) {
+ for(String wheel : wheelParams) {
+ LOGGER.info(wheel);
+ }
+ }
+
+ try {
+ Thread.sleep(10000); //1000 milliseconds is one second.
+ } catch(InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ }
+
+
+ return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
+ }
+
+ @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
+ produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
+ public ResponseEntity<Car> create(@RequestBody Car car) {
+ long count = counter.incrementAndGet();
+ HttpHeaders headers = new HttpHeaders();
+ headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
+
+ return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);
+ }
+
+}
+++ /dev/null
-package rest;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
-}
+++ /dev/null
-package rest;
-
-public class Car {
-
- private final long id;
- private final String content;
-
- public Car(long id, String content) {
- this.id = id;
- this.content = content;
- }
-
- public long getId() {
- return id;
- }
-
- public String getContent() {
- return content;
- }
-}
+++ /dev/null
-package rest;
-
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.bind.annotation.RestController;
-
-@RestController
-@RequestMapping("/api/cars/")
-public class CarController {
-
- private static final String template = "Car: %s";
- private final AtomicLong counter = new AtomicLong();
-
- @RequestMapping(produces = { "application/json" }, method = RequestMethod.GET)
- @ResponseStatus(HttpStatus.OK)
- public List<Car> cars() {
- final List<Car> cars = new ArrayList<>();
- cars.add(new Car(counter.incrementAndGet(), String.format(template, 1)));
- cars.add(new Car(counter.incrementAndGet(), String.format(template, 2)));
- cars.add(new Car(counter.incrementAndGet(), String.format(template, 3)));
-
- return cars;
- }
-
- @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
- @ResponseStatus(HttpStatus.OK)
- public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
- @PathVariable("id") long id,
- @RequestParam Map<String, String> params,
- @RequestParam(value = "wheel", required = false) String[] wheelParams) {
-
- if (specialHeader != null) {
- System.out.println("SPECIAL HEADER: " + specialHeader);
- }
-
- if (params.get("mirror") != null) {
- System.out.println("MIRROR: " + params.get("mirror"));
- }
-
- if (params.get("window") != null) {
- System.out.println("WINDOW: " + params.get("window"));
- }
-
- if (wheelParams != null) {
- for(String wheel : wheelParams) {
- System.out.println(wheel);
- }
- }
-
- try {
- Thread.sleep(10000); //1000 milliseconds is one second.
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
-
-
- return new Car(counter.incrementAndGet(), String.format(template, id));
- }
-
- @RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
- public ResponseEntity<Car> create() {
- HttpHeaders headers = new HttpHeaders();
- headers.add("Location", "/api/cars/" + 1);
-
- return new ResponseEntity<>(new Car(counter.incrementAndGet(), String.format(template, 1)), headers, HttpStatus.CREATED);
- }
-
-}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ status: The level of internal Log4j events that should be logged to the console.
+ Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal".
+
+ monitorInterval: The minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes.
+
+
+ see https://logging.apache.org/log4j/2.x/manual/configuration.html
+ -->
+<Configuration status="error" strict="true" monitorInterval="30"
+ name="XMLConfigTest" packages="org.apache.logging.log4j.test">
+
+ <!--
+ ALL > TRACE > DEBUG > INFO > WARN > ERROR > OFF
+
+ ERROR by default.
+ -->
+
+ <Appenders>
+ <Appender type="Console" name="STDOUT">
+ <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
+ </Appender>
+ </Appenders>
+ <Loggers>
+
+ <!--
+ Specific log level for Spring WEBMVC.
+ -->
+ <Logger name="org.springframework.web" level="INFO" additivity="false">
+ <AppenderRef ref="STDOUT" />
+ </Logger>
+
+
+ <!--
+ General logging Spring.
+ -->
+ <Logger name="org.springframework" level="INFO" additivity="false">
+ <AppenderRef ref="STDOUT" />
+ </Logger>
+
+
+ <!--
+ Anything else will be using TRACE logging level.
+ -->
+ <Root level="INFO">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+
+ </Loggers>
+</Configuration>
--- /dev/null
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+ xmlns:mvc="http://www.springframework.org/schema/mvc"\r
+ xmlns:context="http://www.springframework.org/schema/context"\r
+ xmlns:util="http://www.springframework.org/schema/util"\r
+ xmlns:p="http://www.springframework.org/schema/p"\r
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\r
+ http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd\r
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd\r
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">\r
+ \r
+ <context:annotation-config />\r
+ \r
+ <context:component-scan base-package="de.spring.webservices.rest"/>\r
+\r
+ <bean id="jsonObjectMapperFactory" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:indentOutput="true" p:failOnEmptyBeans="false">\r
+ <property name="featuresToDisable">\r
+ <array>\r
+ <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>\r
+ <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION"/>\r
+ </array>\r
+ </property>\r
+ </bean>\r
+\r
+ <util:list id="messageConverters">\r
+ <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" p:objectMapper-ref="jsonObjectMapperFactory"/>\r
+ <bean class="org.springframework.http.converter.StringHttpMessageConverter" />\r
+ </util:list>\r
+\r
+\r
+ <bean name="handlerAdapter"\r
+ class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">\r
+ <property name="webBindingInitializer">\r
+ <bean\r
+ class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">\r
+ <!-- It enables us to use JSR-303 -->\r
+ <property name="validator">\r
+ <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>\r
+ </property>\r
+ </bean>\r
+ </property>\r
+ <property name="messageConverters" ref="messageConverters" />\r
+ <property name="responseBodyAdvice">\r
+ <util:list>\r
+ <bean id="responseBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice"/>\r
+ </util:list>\r
+ </property>\r
+ </bean>\r
+ \r
+ <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>\r
+\r
+ <mvc:default-servlet-handler />\r
+ \r
+</beans>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+ xmlns:context="http://www.springframework.org/schema/context"\r
+ xmlns:util="http://www.springframework.org/schema/util"\r
+ xsi:schemaLocation="http://www.springframework.org/schema/beans\r
+ http://www.springframework.org/schema/beans/spring-beans.xsd\r
+ http://www.springframework.org/schema/context\r
+ http://www.springframework.org/schema/context/spring-context.xsd\r
+ http://www.springframework.org/schema/util\r
+ http://www.springframework.org/schema/util/spring-util.xsd">\r
+\r
+</beans>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
+ version="2.4">
+
+ <display-name>Spring REST Services: example</display-name>
+
+ <listener>
+ <listener-class>
+ org.springframework.web.context.ContextLoaderListener
+ </listener-class>
+ </listener>
+
+ <context-param>
+ <param-name>spring.profiles.active</param-name>
+ <param-value>${environment.profile}</param-value>
+ <param-name>contextConfigLocation</param-name>
+ <param-value>
+ classpath*:spring-configuration/*.xml
+ </param-value>
+ </context-param>
+
+ <!-- Spring REST support -->
+ <servlet>
+ <servlet-name>spring-rest</servlet-name>
+ <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ <async-supported>true</async-supported>
+ <init-param>
+ <param-name>contextConfigLocation</param-name>
+ <param-value>classpath*:spring-configuration/mvc/rest/*.xml</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>spring-rest</servlet-name>
+ <url-pattern>/spring-rest/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>
--- /dev/null
+package de.spring.webservices.rest;
+
+import static org.hamcrest.Matchers.any;
+import static org.hamcrest.Matchers.is;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
+public class CarControllerIntegrationTest {
+ private CarController controller;
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ controller = new CarController();
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
+ }
+
+ @Test
+ public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
+ mockMvc.perform(get("/api/cars/")
+ .accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
+
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$[0].id", any(Integer.class)))
+ .andExpect(jsonPath("$[0].content", is("Car: 1")))
+ .andExpect(jsonPath("$[1].content", is("Car: 2")))
+ .andExpect(jsonPath("$[1].id", any(Integer.class)))
+ .andExpect(jsonPath("$[2].content", is("Car: 3")))
+ .andExpect(jsonPath("$[2].id", any(Integer.class)))
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
+ }
+
+// @Test
+// public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
+// mockMvc.perform(get("/api/cars/{id}", 1L)
+// .accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
+//
+// .andExpect(status().isOk())
+// .andExpect(jsonPath("id", any(Integer.class)))
+// .andExpect(jsonPath("content", is("Car: 1")))
+// .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
+// }
+
+ @Test
+ public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
+ Car car = new Car(2L, "nothing");
+ mockMvc.perform(post("/api/cars/")
+ .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
+ .content(asJsonString(car))
+ .accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
+
+ .andExpect(status().isCreated())
+ .andExpect(jsonPath("id", any(Integer.class)))
+ .andExpect(jsonPath("content", is("Car: 1")))
+ .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
+ }
+
+ private static String asJsonString(final Object obj) throws JsonProcessingException {
+ final ObjectMapper mapper = new ObjectMapper();
+ return mapper.writeValueAsString(obj);
+ }
+}