Inside the 'Web' layer | Files | +Java Config | |
---|---|---|---|
Spring MVC- Atom integration | +Java Config branch | - VetsAtomView.java - mvc-view-config.xml + Petclinic uses XML configuration by default. In case you'd like to use Java Config instead, there is a Java Config branch available here. Thanks to Antoine Rey for his contribution. | |
Inside the 'Web' layer | Files | +||
Spring MVC - XML integration | mvc-view-config.xml | @@ -63,10 +75,6 @@ File -> Import -> Maven -> Existing Maven projectSpring MVC - ContentNegotiatingViewResolver | mvc-view-config.xml |
Spring MVC Test Framework | -VisitsViewTest.java | -||
JSP custom tags | @@ -78,7 +86,7 @@ File -> Import -> Maven -> Existing Maven project |
webjars declaration inside pom.xml Resource mapping in Spring configuration - sample usage in JSP |
+ sample usage in JSP
|
Collection
of matching Owner
s (or an empty Collection
if none
- * found)
+ * found)
*/
CollectionOwner
if found
- * @throws org.springframework.dao.DataRetrievalFailureException
- * if not found
+ * @throws org.springframework.dao.DataRetrievalFailureException if not found
*/
Owner findById(int id) throws DataAccessException;
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java
index 693b2e5e9..1770e8555 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java
@@ -45,8 +45,7 @@ public interface PetRepository {
*
* @param id the id to search for
* @return the Pet
if found
- * @throws org.springframework.dao.DataRetrievalFailureException
- * if not found
+ * @throws org.springframework.dao.DataRetrievalFailureException if not found
*/
Pet findById(int id) throws DataAccessException;
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 75e55a41a..45de1aee0 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
@@ -25,9 +25,9 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
-import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.model.Owner;
@@ -35,7 +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,23 +51,19 @@ 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")
- .usingGeneratedKeyColumns("id");
+ .withTableName("owners")
+ .usingGeneratedKeyColumns("id");
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
- this.visitRepository = visitRepository;
}
@@ -79,12 +74,12 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
*/
@Override
public CollectionValidator
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 validaation + // name validation if (!StringUtils.hasLength(name)) { errors.rejectValue("name", "required", "required"); - } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) { - errors.rejectValue("name", "duplicate", "already exists"); } - - // type valication + + // type validation if (pet.isNew() && pet.getType() == null) { errors.rejectValue("type", "required", "required"); } - - // type valication - if (pet.getBirthDate()==null) { + + // birth date validation + if (pet.getBirthDate() == null) { errors.rejectValue("birthDate", "required", "required"); } } + /** + * This Validator validates *just* Pet instances + */ + @Override + public boolean supports(Class> clazz) { + return Pet.class.isAssignableFrom(clazz); + } + + } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VetController.java b/src/main/java/org/springframework/samples/petclinic/web/VetController.java index 3a6c052ea..3121e60e8 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VetController.java @@ -22,6 +22,7 @@ import org.springframework.samples.petclinic.model.Vets; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; /** * @author Juergen Hoeller @@ -40,9 +41,9 @@ public class VetController { this.clinicService = clinicService; } - @RequestMapping("/vets") + @RequestMapping(value = {"/vets.xml", "/vets.html"}) public String showVetList(Map
+ ![]() Sergiu Bodiu+ +Java Consultant at Bank of America+ Seasoned consultant experienced in large-scale e-commerce projects, passionate about + providing innovative technology solutions to solve complex business problems, have extensive knowledge and + experience delivering enterprise wide applications. He is skilled in software design, data modeling, + stakeholder management, IT strategic planning, technical know-how and security. Able to design, implement, + test and maintain software product components with strong focus on design elegance and software reuse. + |
+
+ ![]() Sergiu Bodiu+ +Java Consultant at Bank of America+ Seasoned consultant experienced in large-scale e-commerce projects, passionate about + providing innovative technology solutions to solve complex business problems, have extensive knowledge and + experience delivering enterprise wide applications. He is skilled in software design, data modeling, + stakeholder management, IT strategic planning, technical know-how and security. Able to design, implement, + test and maintain software product components with strong focus on design elegance and software reuse. + |
+
- | + |
">View as XML | - ">Subscribe to Atom feed + ">View as JSon |
{@link
- * AbstractclinicServiceTests#clinicService clinicService}
instance variable, which uses autowiring by
+ * AbstractClinicServiceTests#clinicService clinicService} instance variable, which uses autowiring by
* type. Integration test using the jdbc profile. - * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
+ *Integration test using the jdbc profile. * * @author Thomas Risberg * @author Michael Isvy + * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
*/ @ContextConfiguration(locations = {"classpath:spring/business-config.xml"}) @RunWith(SpringJUnit4ClassRunner.class) diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java index e024f21fd..dba2b195a 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java @@ -1,4 +1,3 @@ - package org.springframework.samples.petclinic.service; import org.junit.runner.RunWith; @@ -7,12 +6,12 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - *Integration test using the jpa profile. - * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
+ *Integration test using the jpa profile. * * @author Rod Johnson * @author Sam Brannen * @author Michael Isvy + * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
*/ @ContextConfiguration(locations = {"classpath:spring/business-config.xml"}) @@ -20,4 +19,4 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ActiveProfiles("jpa") public class ClinicServiceJpaTests extends AbstractClinicServiceTests { -} \ No newline at end of file +} diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java index e01dda551..31b62289f 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java @@ -1,4 +1,3 @@ - package org.springframework.samples.petclinic.service; import org.junit.runner.RunWith; @@ -7,9 +6,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - *Integration test using the 'Spring Data' profile. - * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
+ *Integration test using the 'Spring Data' profile. + * * @author Michael Isvy + * @see AbstractClinicServiceTests AbstractClinicServiceTests for more details.
*/ @ContextConfiguration(locations = {"classpath:spring/business-config.xml"}) @@ -17,4 +17,4 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ActiveProfiles("spring-data-jpa") public class ClinicServiceSpringDataJpaTests extends AbstractClinicServiceTests { -} \ No newline at end of file +} diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java new file mode 100644 index 000000000..28b9348a1 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java @@ -0,0 +1,48 @@ +package org.springframework.samples.petclinic.web; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +/** + * Test class for the UserResource REST controller. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@WebAppConfiguration +@ActiveProfiles("spring-data-jpa") +public class VetControllerTests { + + @Autowired + private VetController vetController; + + private MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(vetController).build(); + } + + @Test + public void testGetExistingUser() throws Exception { + ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + actions.andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(jsonPath("$.vetList[0].id").value(1)); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml deleted file mode 100644 index 8458ba2ec..000000000 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml +++ /dev/null @@ -1,13 +0,0 @@ - - -true
+ true
+