Skip to content

Commit 04ca150

Browse files
committed
Initial commit
0 parents  commit 04ca150

10 files changed

+224
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Maven
2+
target
3+
4+
# Eclipse
5+
.project
6+
.settings
7+
.classpath
8+
9+
# IntelliJ IDEA
10+
.idea
11+
12+
*.iml

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# REST Service in Spring Boot
2+
3+
Simple example of a REST Service in Spring Boot.
4+
5+
To run this project
6+
```bash
7+
mvn spring-boot:run
8+
```
9+

app-build-deploy.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mvn spring-boot:run

pom.xml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.kaluzny</groupId>
7+
<artifactId>spring-boot-rest-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>REST Service in Spring Boot</name>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.4.0.RELEASE</version>
17+
<relativePath/>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-rest</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-data-jpa</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
</dependency>
49+
50+
</dependencies>
51+
52+
<build>
53+
<finalName>REST Service in Spring Boot</finalName>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.kaluzny;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.kaluzny.domain;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Book {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private Long id;
14+
private String name;
15+
private String description;
16+
17+
public Book(Long id, String name, String description) {
18+
this.id = id;
19+
this.name = name;
20+
this.description = description;
21+
}
22+
23+
public Book() {
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public String getDescription() {
35+
return description;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kaluzny.domain;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5+
6+
import java.util.List;
7+
8+
@RepositoryRestResource
9+
public interface BookRepository extends CrudRepository<Book, Long> {
10+
List<Book> findByName(String name);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.kaluzny.web;
2+
3+
import com.kaluzny.domain.Book;
4+
import com.kaluzny.domain.BookRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.Collection;
11+
12+
@RestController
13+
@RequestMapping("/books")
14+
public class BookController {
15+
16+
private BookRepository repository;
17+
18+
@Autowired
19+
public void setRepository(BookRepository repository) {
20+
this.repository = repository;
21+
}
22+
23+
@RequestMapping(
24+
method = RequestMethod.GET)
25+
public ResponseEntity<Collection<Book>> getAllBooks() {
26+
return new ResponseEntity<>((Collection<Book>) repository.findAll(), HttpStatus.OK);
27+
}
28+
29+
@RequestMapping(
30+
method = RequestMethod.GET,
31+
value = "/{id}")
32+
public ResponseEntity<Book> getBookWithId(@PathVariable Long id) {
33+
return new ResponseEntity<>(repository.findOne(id), HttpStatus.OK);
34+
}
35+
36+
@RequestMapping(
37+
method = RequestMethod.GET,
38+
params = {"name"})
39+
public ResponseEntity<Collection<Book>> findBookWithName(@RequestParam(value = "name") String name) {
40+
return new ResponseEntity<>(repository.findByName(name), HttpStatus.OK);
41+
}
42+
43+
@RequestMapping(
44+
method = RequestMethod.POST)
45+
public ResponseEntity<?> addBook(@RequestBody Book book) {
46+
return new ResponseEntity<>(repository.save(book), HttpStatus.CREATED);
47+
}
48+
49+
@RequestMapping(
50+
method = RequestMethod.DELETE,
51+
value = "/{id}")
52+
public void deleteBookWithId(@PathVariable Long id) {
53+
repository.delete(id);
54+
}
55+
56+
@RequestMapping(
57+
method = RequestMethod.DELETE)
58+
public void deleteAllBooks() {
59+
repository.deleteAll();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.kaluzny;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.test.context.web.WebAppConfiguration;
6+
import org.springframework.boot.test.SpringApplicationConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
@RunWith(SpringJUnit4ClassRunner.class)
10+
@SpringApplicationConfiguration(classes = Application.class)
11+
@WebAppConfiguration
12+
public class SpringBootRestExampleApplicationTests {
13+
14+
@Test
15+
public void contextLoads() {
16+
}
17+
18+
}

0 commit comments

Comments
 (0)