REST: Spring REST project for trying out AngularJS $http
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 25 Aug 2015 18:57:49 +0000 (20:57 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 8 Jan 2016 21:46:16 +0000 (22:46 +0100)
SpringJava/REST/README [new file with mode: 0644]
SpringJava/REST/build.gradle [new file with mode: 0644]
SpringJava/REST/src/main/java/rest/Application.java [new file with mode: 0644]
SpringJava/REST/src/main/java/rest/Car.java [new file with mode: 0644]
SpringJava/REST/src/main/java/rest/CarController.java [new file with mode: 0644]

diff --git a/SpringJava/REST/README b/SpringJava/REST/README
new file mode 100644 (file)
index 0000000..cb44e7d
--- /dev/null
@@ -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 (file)
index 0000000..184029f
--- /dev/null
@@ -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 (file)
index 0000000..808c3ab
--- /dev/null
@@ -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 (file)
index 0000000..06edef1
--- /dev/null
@@ -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 (file)
index 0000000..bb45725
--- /dev/null
@@ -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<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 = { "application/json" }, method = RequestMethod.GET)
+    @ResponseStatus(HttpStatus.OK)
+    public Car car(@PathVariable("id") long id) {
+        return new Car(counter.incrementAndGet(), String.format(template, id));
+    }
+}