From 7ef6ccff5a6fc6552b044e653e34aa18fb242a6d Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 25 Aug 2015 20:57:49 +0200 Subject: [PATCH] REST: Spring REST project for trying out AngularJS $http --- SpringJava/REST/README | 2 ++ SpringJava/REST/build.gradle | 33 +++++++++++++++++++ .../REST/src/main/java/rest/Application.java | 12 +++++++ SpringJava/REST/src/main/java/rest/Car.java | 20 ++++++++++++ .../REST/src/main/java/rest/CarController.java | 38 ++++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 SpringJava/REST/README create mode 100644 SpringJava/REST/build.gradle create mode 100644 SpringJava/REST/src/main/java/rest/Application.java create mode 100644 SpringJava/REST/src/main/java/rest/Car.java create mode 100644 SpringJava/REST/src/main/java/rest/CarController.java diff --git a/SpringJava/REST/README b/SpringJava/REST/README new file mode 100644 index 0000000..cb44e7d --- /dev/null +++ b/SpringJava/REST/README @@ -0,0 +1,2 @@ +gradle build +java -jar build/libs/rest-example-0.0.1.jar diff --git a/SpringJava/REST/build.gradle b/SpringJava/REST/build.gradle new file mode 100644 index 0000000..184029f --- /dev/null +++ b/SpringJava/REST/build.gradle @@ -0,0 +1,33 @@ +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE") + } +} + +apply plugin: 'java' +apply plugin: 'eclipse' +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' +} diff --git a/SpringJava/REST/src/main/java/rest/Application.java b/SpringJava/REST/src/main/java/rest/Application.java new file mode 100644 index 0000000..808c3ab --- /dev/null +++ b/SpringJava/REST/src/main/java/rest/Application.java @@ -0,0 +1,12 @@ +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); + } +} diff --git a/SpringJava/REST/src/main/java/rest/Car.java b/SpringJava/REST/src/main/java/rest/Car.java new file mode 100644 index 0000000..06edef1 --- /dev/null +++ b/SpringJava/REST/src/main/java/rest/Car.java @@ -0,0 +1,20 @@ +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; + } +} diff --git a/SpringJava/REST/src/main/java/rest/CarController.java b/SpringJava/REST/src/main/java/rest/CarController.java new file mode 100644 index 0000000..bb45725 --- /dev/null +++ b/SpringJava/REST/src/main/java/rest/CarController.java @@ -0,0 +1,38 @@ +package rest; + +import java.util.concurrent.atomic.AtomicLong; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PathVariable; +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 cars() { + final List 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 = { "application/json" }, method = RequestMethod.GET) + @ResponseStatus(HttpStatus.OK) + public Car car(@PathVariable("id") long id) { + return new Car(counter.incrementAndGet(), String.format(template, id)); + } +} -- 2.1.4