Skip to content

Commit 0de94d5

Browse files
author
YusukeHasegawa
committed
https://github.com/spring-projects/spring-framework/issues/15682
1 parent e4f427b commit 0de94d5

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/docs/openapi/petstore.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ paths:
2121
schema:
2222
type: integer
2323
format: int32
24+
maximum: 100
2425
responses:
2526
'200':
2627
description: A paged array of pets

src/main/java/com/example/openapi/web/api/PetsApiController.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
import lombok.RequiredArgsConstructor;
1010
import lombok.experimental.FieldDefaults;
1111
import org.springframework.http.ResponseEntity;
12-
import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.bind.annotation.CrossOrigin;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RestController;
1315
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
1416
import org.zalando.problem.Problem;
1517
import org.zalando.problem.Status;
16-
import org.zalando.problem.StatusType;
1718

1819
import javax.validation.Valid;
20+
import javax.validation.constraints.Max;
1921
import java.util.List;
20-
import java.util.Optional;
2122
import java.util.concurrent.atomic.AtomicInteger;
2223
import java.util.stream.Collectors;
2324

@@ -33,6 +34,8 @@ public class PetsApiController implements PetsApi {
3334

3435
AtomicInteger i = new AtomicInteger();
3536

37+
//https://github.com/spring-projects/spring-framework/issues/15682
38+
//5.1でsuper classのアノテーション見てくれるようになった模様 @RequestBody はなくてもよい
3639
@Override
3740
public ResponseEntity<Void> createPets2(@Valid @RequestBody final NewPet newPet) {
3841
final Pets pet = petsRepository.save(petMapper.newPetToPets(newPet));
@@ -42,6 +45,11 @@ public ResponseEntity<Void> createPets2(@Valid @RequestBody final NewPet newPet)
4245
.build();
4346
}
4447

48+
@Override
49+
public ResponseEntity<List<Pet>> listPets(@Max(100) @Valid final Integer limit) {
50+
return ResponseEntity.ok(petsRepository.findAll().stream().map(petMapper::petsToPet).collect(Collectors.toList()));
51+
}
52+
4553
@Override
4654
public ResponseEntity<Void> createPets() {
4755
final Pets entity = new Pets();
@@ -53,21 +61,14 @@ public ResponseEntity<Void> createPets() {
5361
.build();
5462
}
5563

56-
@Override
57-
public ResponseEntity<List<Pet>> listPets(
58-
@Valid @RequestParam(value = "limit", required = false) final Integer limit) {
59-
return ResponseEntity.ok(petsRepository.findAll().stream().map(petMapper::petsToPet).collect(Collectors.toList()));
60-
61-
}
6264

6365
@Override
64-
public ResponseEntity<List<Pet>> showPetById(
65-
@PathVariable("petId") final String petId) {
66+
public ResponseEntity<List<Pet>> showPetById(final String petId) {
6667
final List<Pets> pets = petsRepository.findByName(petId);
6768

68-
if(!pets.isEmpty()){
69+
if (!pets.isEmpty()) {
6970
return ResponseEntity.ok(pets.stream().map(petMapper::petsToPet).collect(Collectors.toList()));
70-
}else{
71+
} else {
7172
throw Problem.valueOf(Status.NOT_FOUND);
7273
}
7374
}

0 commit comments

Comments
 (0)