diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java index 1a20bf4ac..1270af8d3 100755 --- a/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java @@ -23,6 +23,7 @@ public interface PetMapper { Collection toPets(Collection pets); + @Mapping(source = "ownerId", target = "owner.id") Pet toPet(PetDto petDto); Pet toPet(PetFieldsDto petFieldsDto); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java index 62767fca2..7960c6d20 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java @@ -13,6 +13,7 @@ */ @Mapper(uses = PetMapper.class) public interface VisitMapper { + @Mapping(source = "petId", target = "pet.id") Visit toVisit(VisitDto visitDto); Visit toVisit(VisitFieldsDto visitFieldsDto); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java index c249d3aab..e5727fda6 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java @@ -16,6 +16,7 @@ package org.springframework.samples.petclinic.rest.controller; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.samples.petclinic.mapper.PetMapper; @@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UriComponentsBuilder; import java.util.ArrayList; import java.util.List; @@ -98,7 +100,10 @@ public ResponseEntity deletePet(Integer petId) { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @Override public ResponseEntity addPet(PetDto petDto) { - this.clinicService.savePet(petMapper.toPet(petDto)); - return new ResponseEntity<>(petDto, HttpStatus.OK); + HttpHeaders headers = new HttpHeaders(); + Pet pet = petMapper.toPet(petDto); + this.clinicService.savePet(pet); + headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}").buildAndExpand(pet.getId()).toUri()); + return new ResponseEntity<>(petMapper.toPetDto(pet), headers, HttpStatus.CREATED); } } diff --git a/src/main/resources/openapi.yml b/src/main/resources/openapi.yml index 7edeb2cb1..792cfa072 100755 --- a/src/main/resources/openapi.yml +++ b/src/main/resources/openapi.yml @@ -812,7 +812,7 @@ paths: $ref: '#/components/schemas/Pet' required: true responses: - 200: + 201: description: Pet type created successfully. headers: ETag: diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java index a4bce2b9f..c6029feb0 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.PetMapper; import org.springframework.samples.petclinic.model.Pet; @@ -229,7 +230,8 @@ void testAddPetSuccess() throws Exception { given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); this.mockMvc.perform(post("/api/pets") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isOk()); + .andExpect(status().isCreated()) + .andExpect(header().string(HttpHeaders.LOCATION, "/api/pets/3")); } @Test