Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions spring-boot-modules/spring-boot-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.boot.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.restclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.restclient;

public class Article {
private Integer id;
private String title;

public Article() {
}

public Article(Integer id, String title) {
this.id = id;
this.title = title;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.baeldung.restclient;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/articles")
class ArticleController {

Map<Integer, Article> database = new HashMap<>();

@GetMapping
ResponseEntity<Collection<Article>> getArticles() {
Collection<Article> values = database.values();
if (values.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(values);
}

@GetMapping("/{id}")
ResponseEntity<Article> getArticle(@PathVariable("id") Integer id) {
Article article = database.get(id);
if (article == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(article);
}

@GetMapping(value = "/{id}", headers = "API-Version=2")
ResponseEntity<Article> getArticleV2(@PathVariable("id") Integer id) {
return ResponseEntity.ok(new Article(100, "SECRET ARTICLE"));
}

@GetMapping("/search")
ResponseEntity<Article> searchArticleByTitle(@RequestParam(name = "title") String title) {
Optional<Article> article = database.values().stream()
.filter(a -> a.getTitle().contains(title))
.findFirst();
if (article.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(article.get());
}

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
void createArticle(@RequestBody Article article) {
database.put(article.getId(), article);
}

@PutMapping("/{id}")
void updateArticle(@PathVariable("id") Integer id, @RequestBody Article article) {
assert Objects.equals(id, article.getId());
database.remove(id);
database.put(id, article);
}

@DeleteMapping("/{id}")
void deleteArticle(@PathVariable("id") Integer id) {
database.remove(id);
}

@DeleteMapping
void deleteAllArticles() {
database.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.restclient;

class ArticleNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.restclient;

class InvalidArticleResponseException extends RuntimeException {
}
Loading