diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index 4bc2b92f7..f760a9485 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -36,6 +36,9 @@ import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.format.annotation.DateTimeFormat; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * Simple business object representing a pet. * @@ -58,6 +61,7 @@ public class Pet extends NamedEntity { @ManyToOne @JoinColumn(name = "owner_id") + @JsonIgnore private Owner owner; @OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER) diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/model/Visit.java index ea03bde74..181830abc 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Visit.java @@ -26,6 +26,8 @@ import org.hibernate.validator.constraints.NotEmpty; import org.joda.time.DateTime; import org.springframework.format.annotation.DateTimeFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * Simple JavaBean domain object representing a visit. * @@ -55,6 +57,7 @@ public class Visit extends BaseEntity { */ @ManyToOne @JoinColumn(name = "pet_id") + @JsonIgnore private Pet pet; diff --git a/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java new file mode 100644 index 000000000..a4e155804 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.rest; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author lim.han + */ +@RestController +@RequestMapping("/api") +public class OwnerRestController { + + private final ClinicService clinicService; + + @Autowired + public OwnerRestController(ClinicService clinicService) { + this.clinicService = clinicService; + } + + @RequestMapping(value = "/owners", method = RequestMethod.POST) + public @ResponseBody Owner create(@RequestBody Owner owner) { + this.clinicService.saveOwner(owner); + return owner; + } + + @RequestMapping(value = "/owners", method = RequestMethod.PUT) + public @ResponseBody Collection create(@RequestBody Collection owners) { + for(Owner owner : owners) { + this.clinicService.saveOwner(owner); + } + + return owners; + } + + @RequestMapping(value = "/owners/{id}", method = RequestMethod.GET) + public @ResponseBody Owner find(@PathVariable Integer id) { + return this.clinicService.findOwnerById(id); + } + + @RequestMapping(value = "/owners", method = RequestMethod.GET) + public @ResponseBody Collection findByLastName(@RequestParam(defaultValue="") String lastName) { + return this.clinicService.findOwnerByLastName(lastName); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java new file mode 100644 index 000000000..89ddcbd9d --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java @@ -0,0 +1,71 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.rest; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author lim.han + */ +@RestController +@RequestMapping("/api") +public class PetRestController { + + private final ClinicService clinicService; + + + @Autowired + public PetRestController(ClinicService clinicService) { + this.clinicService = clinicService; + } + + @RequestMapping(value = "/pets/types", method = RequestMethod.GET) + public Collection populatePetTypes() { + return this.clinicService.findPetTypes(); + } + + @RequestMapping(value = "/owners/{ownerId}/pets", method = RequestMethod.POST) + public Pet addPet(@PathVariable("ownerId") int ownerId, @RequestBody Pet pet) { + Owner owner = this.clinicService.findOwnerById(ownerId); + owner.addPet(pet); + this.clinicService.savePet(pet); + return pet; + } + + @RequestMapping(value = "/owners/{ownerId}/pets", method = RequestMethod.PUT) + public Collection addPets(@PathVariable("ownerId") int ownerId, @RequestBody Collection pets) { + Owner owner = this.clinicService.findOwnerById(ownerId); + + for(Pet pet : pets) { + owner.addPet(pet); + this.clinicService.savePet(pet); + } + + return pets; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java new file mode 100644 index 000000000..6c7d6f62d --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.rest; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Juergen Hoeller + * @author Mark Fisher + * @author Ken Krebs + * @author Arjen Poutsma + */ +@RestController +@RequestMapping(value="/api") +public class VetRestController { + + private final ClinicService clinicService; + + + @Autowired + public VetRestController(ClinicService clinicService) { + this.clinicService = clinicService; + } + + @RequestMapping(value = "/vets", method = RequestMethod.GET) + public Collection getVets() { + return this.clinicService.findVets(); + } + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java new file mode 100644 index 000000000..2fa88773c --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.rest; + +import java.util.List; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@RestController +@RequestMapping("/api") +public class VisitRestController { + + private final ClinicService clinicService; + + @Autowired + public VisitRestController(ClinicService clinicService) { + this.clinicService = clinicService; + } + + // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called + @RequestMapping(value = "/pets/{petId}/visits", method = RequestMethod.POST) + public Visit createVisit(@PathVariable int ownerId, @PathVariable int petId, @Valid Visit visit) { + this.clinicService.saveVisit(visit); + return visit; + } + + @RequestMapping(value = "/pets/{petId}/visits", method = RequestMethod.GET) + public List showVisits(@PathVariable int petId) { + return this.clinicService.findPetById(petId).getVisits(); + } + +} diff --git a/src/main/resources/spring/mvc-core-config.xml b/src/main/resources/spring/mvc-core-config.xml index eab2062dc..396fcff10 100644 --- a/src/main/resources/spring/mvc-core-config.xml +++ b/src/main/resources/spring/mvc-core-config.xml @@ -6,13 +6,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" + xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" - xsi:schemaLocation="http://www.springframework.org/schema/mvc - http://www.springframework.org/schema/mvc/spring-mvc.xsd - http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context.xsd"> + xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> @@ -20,9 +19,30 @@ - POJOs labeled with the @Controller and @Service annotations are auto-detected. --> + base-package="org.springframework.samples.petclinic.web, org.springframework.samples.petclinic.rest"/> - + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/spring/mvc-view-config.xml b/src/main/resources/spring/mvc-view-config.xml index 205f7ffce..209076f4c 100644 --- a/src/main/resources/spring/mvc-view-config.xml +++ b/src/main/resources/spring/mvc-view-config.xml @@ -5,7 +5,7 @@