diff --git a/pom.xml b/pom.xml
index f460463f1..50f5b5258 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,50 +11,24 @@
- 1.8
+ 1.7
UTF-8
UTF-8
- 4.1.6.RELEASE
- 1.8.0.RELEASE
+ 1.1.3.RELEASE
+ 1.1.0.RELEASE
- 2.2
- 1.2
- 7.0.47
- 2.2.7
-
-
- 4.3.8.Final
-
-
- 4.3.1.Final
-
-
- 7.0.42
- 2.6.10
- 2.3.2
-
-
- 1.8.5
-
-
- 1.1.3
- 1.7.12
-
-
- 2.0.0
+ 7.0.59
- 4.12
- 3.0.0
+ 2.1.0
1.3
1.1.1
- 2.7
3.2.0.GA
@@ -62,14 +36,25 @@
2.3.0
1.10.3
2.0.3-1
- 0.10.1
-
- 5.1.22
+ 1.0.1
2.7
+
+
+
+
+ io.spring.platform
+ platform-bom
+ ${spring-io-platform.version}
+ pom
+ import
+
+
+
+
org.jadira.usertype
@@ -79,106 +64,83 @@
org.apache.tomcat
tomcat-servlet-api
- ${tomcat.servlet.version}
+ ${tomcat.version}
provided
- javax.servlet.jsp
- jsp-api
- 2.1
- provided
-
+ javax.servlet.jsp
+ javax.servlet.jsp-api
+ provided
+
+
+ org.apache.tomcat
+ tomcat-jasper-el
+ ${tomcat.version}
+ provided
+
- org.glassfish.web
- jstl-impl
- 1.2
-
-
- javax.servlet
- servlet-api
-
-
+ javax.servlet.jsp.jstl
+ javax.servlet.jsp.jstl-api
- com.sun.xml.bind
- jaxb-impl
- ${jaxb-impl.version}
- provided
+ org.apache.taglibs
+ taglibs-standard-jstlel
com.jayway.jsonpath
json-path
- ${json-path.version}
test
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
org.springframework.data
- spring-data-jpa
- ${spring-data-jpa.version}
+ spring-data-jdbc-core
+ ${spring-data-jdbc.version}
-
- org.springframework
- *
-
-
+
+ org.springframework
+ *
+
+
org.springframework
spring-jdbc
- ${spring-framework.version}
-
-
- org.springframework
- spring-aop
- ${spring-framework.version}
org.springframework
spring-webmvc
- ${spring-framework.version}
-
-
- org.springframework
- spring-tx
- ${spring-framework.version}
org.springframework
spring-context-support
- ${spring-framework.version}
-
-
- org.springframework
- spring-orm
- ${spring-framework.version}
org.springframework
spring-oxm
- ${spring-framework.version}
-
-
- commons-lang
- commons-lang
-
-
org.aspectj
aspectjrt
- ${aspectj.version}
org.aspectj
aspectjweaver
- ${aspectj.version}
runtime
@@ -189,20 +151,16 @@
org.apache.tomcat
tomcat-jdbc
- ${tomcat-jdbc.version}
org.slf4j
slf4j-api
- ${slf4j.version}
- compile
ch.qos.logback
logback-classic
- ${logback.version}
runtime
@@ -210,7 +168,6 @@
joda-time
joda-time
- ${jodatime.version}
joda-time
@@ -224,37 +181,31 @@
-
org.hsqldb
hsqldb
- ${hsqldb.version}
runtime
-
+
org.hibernate
hibernate-entitymanager
- ${hibernate.version}
org.hibernate
hibernate-validator
- ${hibernate-validator.version}
org.hibernate
hibernate-ehcache
- ${hibernate.version}
net.sf.ehcache
ehcache-core
- ${ehcache.version}
commons-logging
@@ -283,19 +234,18 @@
org.springframework
spring-test
- ${spring-framework.version}
test
junit
junit
- ${junit.version}
test
org.assertj
assertj-core
${assertj.version}
+ test
@@ -407,7 +357,7 @@
cobertura-maven-plugin
${cobertura.version}
-
+
@@ -432,6 +382,7 @@
html
+
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/Owner.java
index 840a965ed..ca7c97ec2 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Owner.java
@@ -91,13 +91,13 @@ public class Owner extends Person {
protected Set getPetsInternal() {
if (this.pets == null) {
- this.pets = new HashSet();
+ this.pets = new HashSet<>();
}
return this.pets;
}
public List getPets() {
- List sortedPets = new ArrayList(getPetsInternal());
+ List sortedPets = new ArrayList<>(getPetsInternal());
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedPets);
}
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..536fe07e6 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java
@@ -94,13 +94,13 @@ public class Pet extends NamedEntity {
protected Set getVisitsInternal() {
if (this.visits == null) {
- this.visits = new HashSet();
+ this.visits = new HashSet<>();
}
return this.visits;
}
public List getVisits() {
- List sortedVisits = new ArrayList(getVisitsInternal());
+ List sortedVisits = new ArrayList<>(getVisitsInternal());
PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false));
return Collections.unmodifiableList(sortedVisits);
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vet.java b/src/main/java/org/springframework/samples/petclinic/model/Vet.java
index c58bd85b2..61c518786 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Vet.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Vet.java
@@ -56,14 +56,14 @@ public class Vet extends Person {
protected Set getSpecialtiesInternal() {
if (this.specialties == null) {
- this.specialties = new HashSet();
+ this.specialties = new HashSet<>();
}
return this.specialties;
}
@XmlElement
public List getSpecialties() {
- List sortedSpecs = new ArrayList(getSpecialtiesInternal());
+ List sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedSpecs);
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vets.java b/src/main/java/org/springframework/samples/petclinic/model/Vets.java
index e8f44a7bc..aaf96b685 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Vets.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Vets.java
@@ -36,7 +36,7 @@ public class Vets {
@XmlElement
public List getVetList() {
if (vets == null) {
- vets = new ArrayList();
+ vets = new ArrayList<>();
}
return vets;
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
index 579de5284..0a471b4d8 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
@@ -35,8 +35,6 @@ import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
-import org.springframework.samples.petclinic.repository.VisitRepository;
-import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.stereotype.Repository;
/**
@@ -52,15 +50,12 @@ import org.springframework.stereotype.Repository;
@Repository
public class JdbcOwnerRepositoryImpl implements OwnerRepository {
- private VisitRepository visitRepository;
-
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private SimpleJdbcInsert insertOwner;
@Autowired
- public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate,
- VisitRepository visitRepository) {
+ public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.insertOwner = new SimpleJdbcInsert(dataSource)
.withTableName("owners")
@@ -68,7 +63,6 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
- this.visitRepository = visitRepository;
}
@@ -79,7 +73,7 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
*/
@Override
public Collection findByLastName(String lastName) throws DataAccessException {
- Map params = new HashMap();
+ Map params = new HashMap<>();
params.put("lastName", lastName + "%");
List owners = this.namedParameterJdbcTemplate.query(
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
@@ -98,7 +92,7 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
public Owner findById(int id) throws DataAccessException {
Owner owner;
try {
- Map params = new HashMap();
+ Map params = new HashMap<>();
params.put("id", id);
owner = this.namedParameterJdbcTemplate.queryForObject(
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
@@ -113,21 +107,15 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
}
public void loadPetsAndVisits(final Owner owner) {
- Map params = new HashMap();
- params.put("id", owner.getId().intValue());
+ Map params = new HashMap<>();
+ params.put("id", owner.getId());
final List pets = this.namedParameterJdbcTemplate.query(
- "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
+ "SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
params,
- new JdbcPetRowMapper()
+ new JdbcPetVisitExtractor()
);
for (JdbcPet pet : pets) {
owner.addPet(pet);
- // Pet types have not been loaded at this stage. They are loaded separately
- pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
- List visits = this.visitRepository.findByPetId(pet.getId());
- for (Visit visit : visits) {
- pet.addVisit(visit);
- }
}
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
index 546451dcc..e3c02fc93 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
@@ -73,7 +73,7 @@ public class JdbcPetRepositoryImpl implements PetRepository {
@Override
public List findPetTypes() throws DataAccessException {
- Map params = new HashMap();
+ Map params = new HashMap<>();
return this.namedParameterJdbcTemplate.query(
"SELECT id, name FROM types ORDER BY name",
params,
@@ -84,14 +84,14 @@ public class JdbcPetRepositoryImpl implements PetRepository {
public Pet findById(int id) throws DataAccessException {
JdbcPet pet;
try {
- Map params = new HashMap();
+ Map params = new HashMap<>();
params.put("id", id);
pet = this.namedParameterJdbcTemplate.queryForObject(
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
params,
new JdbcPetRowMapper());
} catch (EmptyResultDataAccessException ex) {
- throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
+ throw new ObjectRetrievalFailureException(Pet.class, id);
}
Owner owner = this.ownerRepository.findById(pet.getOwnerId());
owner.addPet(pet);
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
index 4164f746f..ad00a7163 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
@@ -20,18 +20,18 @@ import java.sql.SQLException;
import java.util.Date;
import org.joda.time.DateTime;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.RowMapper;
/**
- * {@link BeanPropertyRowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties
+ * {@link RowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties
* of the {@link JdbcPet} class.
*/
-class JdbcPetRowMapper extends BeanPropertyRowMapper {
+class JdbcPetRowMapper implements RowMapper {
@Override
public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException {
JdbcPet pet = new JdbcPet();
- pet.setId(rs.getInt("id"));
+ pet.setId(rs.getInt("pets.id"));
pet.setName(rs.getString("name"));
Date birthDate = rs.getDate("birth_date");
pet.setBirthDate(new DateTime(birthDate));
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java
new file mode 100644
index 000000000..c40786d93
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2002-2015 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.repository.jdbc;
+
+import org.springframework.data.jdbc.core.OneToManyResultSetExtractor;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.springframework.samples.petclinic.model.Visit;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * {@link ResultSetExtractor} implementation by using the
+ * {@link OneToManyResultSetExtractor} of Spring Data Core JDBC Extensions.
+ */
+public class JdbcPetVisitExtractor extends
+ OneToManyResultSetExtractor {
+
+ public JdbcPetVisitExtractor() {
+ super(new JdbcPetRowMapper(), new JdbcVisitRowMapper());
+ }
+
+ @Override
+ protected Integer mapPrimaryKey(ResultSet rs) throws SQLException {
+ return rs.getInt("pets.id");
+ }
+
+ @Override
+ protected Integer mapForeignKey(ResultSet rs) throws SQLException {
+ if (rs.getObject("visits.pet_id") == null) {
+ return null;
+ } else {
+ return rs.getInt("visits.pet_id");
+ }
+ }
+
+ @Override
+ protected void addChild(JdbcPet root, Visit child) {
+ root.addVisit(child);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java
index 9a85bde11..79db917f9 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java
@@ -32,8 +32,7 @@ import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.stereotype.Repository;
/**
- * A simple JDBC-based implementation of the {@link VetRepository} interface. Uses @Cacheable to cache the result of the
- * {@link findAll} method
+ * A simple JDBC-based implementation of the {@link VetRepository} interface.
*
* @author Ken Krebs
* @author Juergen Hoeller
@@ -55,12 +54,10 @@ public class JdbcVetRepositoryImpl implements VetRepository {
/**
* Refresh the cache of Vets that the ClinicService is holding.
- *
- * @see org.springframework.samples.petclinic.model.service.ClinicService#shouldFindVets()
*/
@Override
public Collection findAll() throws DataAccessException {
- List vets = new ArrayList();
+ List vets = new ArrayList<>();
// Retrieve the list of all vets.
vets.addAll(this.jdbcTemplate.query(
"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
@@ -78,10 +75,10 @@ public class JdbcVetRepositoryImpl implements VetRepository {
new BeanPropertyRowMapper() {
@Override
public Integer mapRow(ResultSet rs, int row) throws SQLException {
- return Integer.valueOf(rs.getInt(1));
+ return rs.getInt(1);
}
},
- vet.getId().intValue());
+ vet.getId());
for (int specialtyId : vetSpecialtiesIds) {
Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
vet.addSpecialty(specialty);
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
index b6a004561..a923b7652 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
@@ -15,17 +15,8 @@
*/
package org.springframework.samples.petclinic.repository.jdbc;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Date;
-import java.util.List;
-
-import javax.sql.DataSource;
-
-import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
@@ -33,6 +24,9 @@ import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Repository;
+import javax.sql.DataSource;
+import java.util.List;
+
/**
* A simple JDBC-based implementation of the {@link VisitRepository} interface.
*
@@ -72,10 +66,6 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
}
}
- public void deletePet(int id) throws DataAccessException {
- this.jdbcTemplate.update("DELETE FROM pets WHERE id=?", id);
- }
-
/**
* Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance.
@@ -90,21 +80,9 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
@Override
public List findByPetId(Integer petId) {
- final List visits = this.jdbcTemplate.query(
- "SELECT id, visit_date, description FROM visits WHERE pet_id=?",
- new BeanPropertyRowMapper() {
- @Override
- public Visit mapRow(ResultSet rs, int row) throws SQLException {
- Visit visit = new Visit();
- visit.setId(rs.getInt("id"));
- Date visitDate = rs.getDate("visit_date");
- visit.setDate(new DateTime(visitDate));
- visit.setDescription(rs.getString("description"));
- return visit;
- }
- },
- petId);
- return visits;
+ return this.jdbcTemplate.query(
+ "SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
+ new JdbcVisitRowMapper(), petId);
}
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java
new file mode 100644
index 000000000..94069042b
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2002-2015 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.repository.jdbc;
+
+
+import org.joda.time.DateTime;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.samples.petclinic.model.Visit;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Date;
+
+/**
+ * {@link RowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties
+ * of the {@link Visit} class.
+ */
+class JdbcVisitRowMapper implements RowMapper {
+
+ @Override
+ public Visit mapRow(ResultSet rs, int row) throws SQLException {
+ Visit visit = new Visit();
+ visit.setId(rs.getInt("visit_id"));
+ Date visitDate = rs.getDate("visit_date");
+ visit.setDate(new DateTime(visitDate));
+ visit.setDescription(rs.getString("description"));
+ return visit;
+ }
+}
diff --git a/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java b/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java
index a18f65c39..41486a5a7 100644
--- a/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java
+++ b/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java
@@ -45,7 +45,7 @@ public abstract class EntityUtils {
public static T getById(Collection entities, Class entityClass, int entityId)
throws ObjectRetrievalFailureException {
for (T entity : entities) {
- if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
+ if (entity.getId() == entityId && entityClass.isInstance(entity)) {
return entity;
}
}
diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java
index ea8aeaaa8..bf823023e 100644
--- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java
+++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java
@@ -34,6 +34,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
+import javax.validation.Valid;
+
/**
* @author Juergen Hoeller
* @author Ken Krebs
@@ -57,8 +59,9 @@ public class PetController {
}
@InitBinder
- public void setAllowedFields(WebDataBinder dataBinder) {
+ public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
+ dataBinder.setValidator(new PetValidator());
}
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET)
@@ -71,8 +74,7 @@ public class PetController {
}
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
- public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
- new PetValidator().validate(pet, result);
+ public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
} else {
@@ -90,9 +92,7 @@ public class PetController {
}
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
- public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
- // we're not using @Valid annotation here because it is easier to define such validation rule in Java
- new PetValidator().validate(pet, result);
+ public String processUpdateForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
} else {
diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java
index 03a1bca7c..0621c98ab 100644
--- a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java
+++ b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java
@@ -18,16 +18,22 @@ package org.springframework.samples.petclinic.web;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
+import org.springframework.validation.Validator;
/**
* Validator
for Pet
forms.
+ *
+ * We're not using Bean Validation annotations here because it is easier to define such validation rule in Java.
+ *
*
* @author Ken Krebs
* @author Juergen Hoeller
*/
-public class PetValidator {
+public class PetValidator implements Validator {
- public void validate(Pet pet, Errors errors) {
+ @Override
+ public void validate(Object obj, Errors errors) {
+ Pet pet = (Pet) obj;
String name = pet.getName();
// name validation
if (!StringUtils.hasLength(name)) {
@@ -47,4 +53,13 @@ public class PetValidator {
}
}
+ /**
+ * This Validator validates *just* Pet instances
+ */
+ @Override
+ public boolean supports(Class> clazz) {
+ return Pet.class.equals(clazz);
+ }
+
+
}
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index 2fcd59eba..9215aa2c5 100644
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -1,9 +1,9 @@
-
+ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+ version="3.0" metadata-complete="true">
Spring PetClinic
Spring PetClinic sample application
@@ -77,6 +77,25 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
dandelionServlet
/dandelion-assets/*
+
+
+
+ encodingFilter
+ org.springframework.web.filter.CharacterEncodingFilter
+
+ encoding
+ UTF-8
+
+
+ forceEncoding
+ true
+
+
+
+
+ encodingFilter
+ /*
+
@@ -100,25 +119,6 @@ see here: http://static.springsource.org/spring/docs/current/spring-framework-re
httpMethodFilter
petclinic
-
-
-
- encodingFilter
- org.springframework.web.filter.CharacterEncodingFilter
-
- encoding
- UTF-8
-
-
- forceEncoding
- true
-
-
-
-
- encodingFilter
- /*
-
diff --git a/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
index 61ed54571..428c285fd 100644
--- a/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
@@ -38,7 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
* TestContext Framework:
- Spring IoC container caching which spares us unnecessary set up
* time between test execution.
- Dependency Injection of test fixture instances, meaning that
* we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the
{@link
- * AbstractclinicServiceTests#clinicService clinicService}
instance variable, which uses autowiring by
+ * AbstractClinicServiceTests#clinicService clinicService} instance variable, which uses autowiring by
* type. - Transaction management, meaning each test method is executed in its own transaction,
* which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
* is no need for a teardown or cleanup script.
- An {@link org.springframework.context.ApplicationContext
diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
index a91df0c3c..ff02afddd 100644
--- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
@@ -25,8 +25,6 @@ import org.springframework.web.context.WebApplicationContext;
/**
* Test class for the UserResource REST controller.
- *
- * @see UserResource
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@@ -38,9 +36,6 @@ public class VetControllerTests {
@Autowired
private VetController vetController;
-
- @Autowired
- private WebApplicationContext ctx;
private MockMvc mockMvc;