mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:35:50 +00:00
LH :: Created the rest controllers for the backend services
This commit is contained in:
parent
7080682d3e
commit
e598094a25
8 changed files with 287 additions and 11 deletions
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
||||
|
|
|
@ -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<Owner> create(@RequestBody Collection<Owner> 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<Owner> findByLastName(@RequestParam(defaultValue="") String lastName) {
|
||||
return this.clinicService.findOwnerByLastName(lastName);
|
||||
}
|
||||
}
|
|
@ -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<PetType> 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<Pet> addPets(@PathVariable("ownerId") int ownerId, @RequestBody Collection<Pet> pets) {
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
|
||||
for(Pet pet : pets) {
|
||||
owner.addPet(pet);
|
||||
this.clinicService.savePet(pet);
|
||||
}
|
||||
|
||||
return pets;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Vet> getVets() {
|
||||
return this.clinicService.findVets();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<Visit> showVisits(@PathVariable int petId) {
|
||||
return this.clinicService.findPetById(petId).getVisits();
|
||||
}
|
||||
|
||||
}
|
|
@ -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">
|
||||
|
||||
<import resource="mvc-view-config.xml"/>
|
||||
|
||||
|
@ -20,9 +19,30 @@
|
|||
- POJOs labeled with the @Controller and @Service annotations are auto-detected.
|
||||
-->
|
||||
<context:component-scan
|
||||
base-package="org.springframework.samples.petclinic.web"/>
|
||||
base-package="org.springframework.samples.petclinic.web, org.springframework.samples.petclinic.rest"/>
|
||||
|
||||
<mvc:annotation-driven conversion-service="conversionService"/>
|
||||
<mvc:annotation-driven conversion-service="conversionService" >
|
||||
<mvc:message-converters>
|
||||
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
|
||||
<property name="objectMapper" >
|
||||
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
|
||||
<property name="featuresToDisable">
|
||||
<array>
|
||||
<util:constant
|
||||
static-field="com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE" />
|
||||
<util:constant
|
||||
static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
|
||||
<util:constant
|
||||
static-field="com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS" />
|
||||
</array>
|
||||
</property>
|
||||
<property name="serializationInclusion" value="NON_NULL" />
|
||||
<property name="simpleDateFormat" value="yyyy-MM-dd'T'HH:mm:ssZ" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</mvc:message-converters>
|
||||
</mvc:annotation-driven>
|
||||
|
||||
<!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files
|
||||
(see header.jsp for more details) -->
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
|
||||
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">
|
||||
<!--
|
||||
- The ContentNegotiatingViewResolver delegates to the InternalResourceViewResolver and BeanNameViewResolver,
|
||||
|
@ -27,7 +27,6 @@
|
|||
<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/>
|
||||
</mvc:view-resolvers>
|
||||
|
||||
|
||||
<!-- Renders an Atom feed of the visits. Used by the BeanNameViewResolver -->
|
||||
<bean id="vets/vetList.atom" class="org.springframework.samples.petclinic.web.VetsAtomView"/>
|
||||
|
||||
|
|
Loading…
Reference in a new issue