add Owner and Vets REST controllers; add @JsonIdentityInfo for avoid infinite loop on serialization

This commit is contained in:
Vitaliy Fedoriv 2016-10-15 20:08:26 +03:00
parent bacbf0a32e
commit 89defddb42
6 changed files with 182 additions and 15 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.Digits; import javax.validation.constraints.Digits;
@ -33,6 +34,9 @@ import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator; import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/** /**
* Simple JavaBean domain object representing an owner. * Simple JavaBean domain object representing an owner.
* *
@ -43,6 +47,7 @@ import org.springframework.core.style.ToStringCreator;
*/ */
@Entity @Entity
@Table(name = "owners") @Table(name = "owners")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Owner extends Person { public class Owner extends Person {
@Column(name = "address") @Column(name = "address")
@NotEmpty @NotEmpty
@ -57,7 +62,7 @@ public class Owner extends Person {
@Digits(fraction = 0, integer = 10) @Digits(fraction = 0, integer = 10)
private String telephone; private String telephone;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
private Set<Pet> pets; private Set<Pet> pets;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,9 +15,12 @@
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.model;
import org.springframework.beans.support.MutableSortDefinition; import java.util.ArrayList;
import org.springframework.beans.support.PropertyComparator; import java.util.Collections;
import org.springframework.format.annotation.DateTimeFormat; import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
@ -27,15 +30,15 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Collections; import org.springframework.beans.support.MutableSortDefinition;
import java.util.Date; import org.springframework.beans.support.PropertyComparator;
import java.util.HashSet; import org.springframework.format.annotation.DateTimeFormat;
import java.util.List;
import java.util.Set; import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/** /**
* Simple business object representing a pet. * Simple business object representing a pet.
@ -46,6 +49,7 @@ import java.util.Set;
*/ */
@Entity @Entity
@Table(name = "pets") @Table(name = "pets")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Pet extends NamedEntity { public class Pet extends NamedEntity {
@Column(name = "birth_date") @Column(name = "birth_date")

View file

@ -0,0 +1,98 @@
/*
* Copyright 2016 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 javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.validation.BindingResult;
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;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @author Vitaliy Fedoriv
*
*/
@RestController
@RequestMapping("/api/owners")
public class OwnerRestController {
@Autowired
private ClinicService clinicService;
@RequestMapping(value = "/}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Owner>> getAllOwners(){
Collection<Owner> owners = this.clinicService.findOwnerByLastName("");
if(owners.isEmpty()){
return new ResponseEntity<Collection<Owner>>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK);
}
@RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){
Owner owner = this.clinicService.findOwnerById(ownerId);
if(owner == null){
return new ResponseEntity<Owner>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Owner>(owner, HttpStatus.OK);
}
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> addOwner(@RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
if(bindingResult.hasErrors() || (owner == null)){
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
}
this.clinicService.saveOwner(owner);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/owners/{id}").buildAndExpand(owner.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
@RequestMapping(value = "/{ownerId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Owner> updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder){
if(bindingResult.hasErrors() || (owner == null)){
return new ResponseEntity<Owner>(HttpStatus.BAD_REQUEST);
}
Owner currentOwner = this.clinicService.findOwnerById(ownerId);
if(currentOwner == null){
return new ResponseEntity<Owner>(HttpStatus.NOT_FOUND);
}
currentOwner.setAddress(owner.getAddress());
currentOwner.setCity(owner.getCity());
currentOwner.setFirstName(owner.getFirstName());
currentOwner.setLastName(owner.getLastName());;
currentOwner.setTelephone(owner.getTelephone());
this.clinicService.saveOwner(currentOwner);
return new ResponseEntity<Owner>(currentOwner, HttpStatus.OK);
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright 2016 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.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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 Vitaliy Fedoriv
*
*/
@RestController
@RequestMapping("api/vets")
public class VetRestController {
@Autowired
private ClinicService clinicService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Vet>> getAllVets(){
Collection<Vet> vets = new ArrayList<Vet>();
vets.addAll(this.clinicService.findVets());
if (vets.isEmpty()){
return new ResponseEntity<Collection<Vet>>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Collection<Vet>>(vets, HttpStatus.OK);
}
}

View file

@ -0,0 +1,5 @@
/**
* The classes in this package represent PetClinic's REST API.
*/
package org.springframework.samples.petclinic.rest;

View file

@ -19,9 +19,10 @@
<!-- <!--
- POJOs labeled with the @Controller and @Service annotations are auto-detected. - POJOs labeled with the @Controller and @Service annotations are auto-detected.
--> -->
<context:component-scan
base-package="org.springframework.samples.petclinic.web"/>
<context:component-scan
base-package="org.springframework.samples.petclinic.web,org.springframework.samples.petclinic.rest"/>
<mvc:annotation-driven conversion-service="conversionService"/> <mvc:annotation-driven conversion-service="conversionService"/>
<!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files <!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files