Skip to content

Commit e3935ee

Browse files
author
Rajeev Kumar Singh
committed
Removed @indexed from Tweet model
1 parent 7bcacaf commit e3935ee

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public Mono<Tweet> createTweets(@Valid @RequestBody Tweet tweet) {
3535
}
3636

3737
@GetMapping("/tweets/{id}")
38-
public Mono<Tweet> getTweetById(@PathVariable(value = "id") String tweetId) {
38+
public Mono<ResponseEntity<Tweet>> getTweetById(@PathVariable(value = "id") String tweetId) {
3939
return tweetRepository.findById(tweetId)
40-
.switchIfEmpty(Mono.error(new TweetNotFoundException(tweetId)));
40+
.map(savedTweet -> ResponseEntity.ok(savedTweet))
41+
.defaultIfEmpty(ResponseEntity.notFound().build());
4142
}
4243

4344
@PutMapping("/tweets/{id}")
@@ -70,14 +71,19 @@ public Flux<Tweet> streamAllTweets() {
7071
}
7172

7273

73-
// Exception Handling Examples
74-
@ExceptionHandler
74+
75+
76+
/*
77+
Exception Handling Examples (These can be put into a @ControllerAdvice to handle exceptions globally)
78+
*/
79+
80+
@ExceptionHandler(DuplicateKeyException.class)
7581
public ResponseEntity handleDuplicateKeyException(DuplicateKeyException ex) {
7682
return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponse("A Tweet with the same text already exists"));
7783
}
7884

79-
@ExceptionHandler
80-
public ResponseEntity handleNotFoundException(TweetNotFoundException ex) {
85+
@ExceptionHandler(TweetNotFoundException.class)
86+
public ResponseEntity handleTweetNotFoundException(TweetNotFoundException ex) {
8187
return ResponseEntity.notFound().build();
8288
}
8389

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example.webfluxdemo.model;
22

33
import org.springframework.data.annotation.Id;
4-
import org.springframework.data.mongodb.core.index.Indexed;
54
import org.springframework.data.mongodb.core.mapping.Document;
65

76
import javax.validation.constraints.NotBlank;
@@ -20,7 +19,6 @@ public class Tweet {
2019

2120
@NotBlank
2221
@Size(max = 140)
23-
@Indexed(unique = true)
2422
private String text;
2523

2624
@NotNull

0 commit comments

Comments
 (0)