Skip to content

Commit 520e448

Browse files
author
Rajeev Kumar Singh
committed
Exception Handling with @ExceptionHandler
1 parent 9a0bf3a commit 520e448

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/main/java/com/example/webfluxdemo/controller/TweetController.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.example.webfluxdemo.controller;
22

3+
import com.example.webfluxdemo.exception.TweetNotFoundException;
34
import com.example.webfluxdemo.model.Tweet;
5+
import com.example.webfluxdemo.payload.ErrorResponse;
46
import com.example.webfluxdemo.repository.TweetRepository;
57
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.dao.DuplicateKeyException;
69
import org.springframework.http.HttpStatus;
710
import org.springframework.http.MediaType;
811
import org.springframework.http.ResponseEntity;
@@ -33,7 +36,8 @@ public Mono<Tweet> createTweets(@Valid @RequestBody Tweet tweet) {
3336

3437
@GetMapping("/tweets/{id}")
3538
public Mono<Tweet> getTweetById(@PathVariable(value = "id") String tweetId) {
36-
return tweetRepository.findById(tweetId);
39+
return tweetRepository.findById(tweetId)
40+
.switchIfEmpty(Mono.error(new TweetNotFoundException("Tweet not found with id "+ tweetId)));
3741
}
3842

3943
@PutMapping("/tweets/{id}")
@@ -65,4 +69,16 @@ public Flux<Tweet> streamAllTweets() {
6569
return tweetRepository.findAll();
6670
}
6771

72+
73+
// Exception Handling Examples
74+
@ExceptionHandler
75+
public ResponseEntity handleDuplicateKeyException(DuplicateKeyException ex) {
76+
return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponse("A Tweet with the same text already exists"));
77+
}
78+
79+
@ExceptionHandler
80+
public ResponseEntity handleNotFoundException(TweetNotFoundException ex) {
81+
return ResponseEntity.notFound().build();
82+
}
83+
6884
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.webfluxdemo.exception;
2+
3+
/**
4+
* Created by rajeevkumarsingh on 22/10/17.
5+
*/
6+
public class TweetNotFoundException extends RuntimeException {
7+
8+
public TweetNotFoundException(String tweetId) {
9+
super("Tweet not found with id " + tweetId);
10+
}
11+
}

src/main/java/com/example/webfluxdemo/model/Tweet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.hibernate.validator.constraints.NotBlank;
44
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.mongodb.core.index.Indexed;
56
import org.springframework.data.mongodb.core.mapping.Document;
67

78
import javax.validation.constraints.NotNull;
@@ -19,6 +20,7 @@ public class Tweet {
1920

2021
@NotBlank
2122
@Size(max = 140)
23+
@Indexed(unique = true)
2224
private String text;
2325

2426
@NotNull
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.webfluxdemo.payload;
2+
3+
/**
4+
* Created by rajeevkumarsingh on 22/10/17.
5+
*/
6+
public class ErrorResponse {
7+
private String message;
8+
9+
public ErrorResponse(String message) {
10+
this.message = message;
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
}

0 commit comments

Comments
 (0)