mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-12 04:51:11 +00:00
Convert to jar with thymeleaf
This commit is contained in:
parent
3450c3d99e
commit
e38a9feebe
65 changed files with 778 additions and 1076 deletions
|
|
@ -18,23 +18,16 @@ package org.springframework.samples.petclinic;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
|
||||
/**
|
||||
* PetClinic Spring Boot Application.
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class PetClinicApplication extends SpringBootServletInitializer {
|
||||
public class PetClinicApplication {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(PetClinicApplication.class);
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(PetClinicApplication.class, args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(PetClinicApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ public class Pet extends NamedEntity {
|
|||
private Owner owner;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
|
||||
private Set<Visit> visits;
|
||||
private Set<Visit> visits = new LinkedHashSet<>();
|
||||
|
||||
|
||||
public void setBirthDate(Date birthDate) {
|
||||
|
|
|
|||
|
|
@ -17,11 +17,9 @@ package org.springframework.samples.petclinic.repository;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
|
||||
/**
|
||||
|
|
@ -51,7 +49,7 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
|
|||
* @return the {@link Owner} if found
|
||||
*/
|
||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
||||
Owner findById(@Param("id") int id);
|
||||
Owner findById(@Param("id") Integer id);
|
||||
|
||||
/**
|
||||
* Save an {@link Owner} to the data store, either inserting or updating it.
|
||||
|
|
|
|||
|
|
@ -17,10 +17,8 @@ package org.springframework.samples.petclinic.repository;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
|
||||
|
|
@ -47,7 +45,7 @@ public interface PetRepository extends Repository<Pet, Integer> {
|
|||
* @param id the id to search for
|
||||
* @return the {@link Pet} if found
|
||||
*/
|
||||
Pet findById(int id);
|
||||
Pet findById(Integer id);
|
||||
|
||||
/**
|
||||
* Save a {@link Pet} to the data store, either inserting or updating it.
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ import javax.cache.annotation.CacheResult;
|
|||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Mostly used as a facade for all Petclinic controllers
|
||||
* Also a placeholder for @Transactional and @CacheResult annotations
|
||||
* Mostly used as a facade for all Petclinic controllers Also a placeholder
|
||||
* for @Transactional and @CacheResult annotations
|
||||
*
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
|
|
@ -43,7 +43,8 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
private VisitRepository visitRepository;
|
||||
|
||||
@Autowired
|
||||
public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository, VisitRepository visitRepository) {
|
||||
public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository,
|
||||
VisitRepository visitRepository) {
|
||||
this.petRepository = petRepository;
|
||||
this.vetRepository = vetRepository;
|
||||
this.ownerRepository = ownerRepository;
|
||||
|
|
@ -74,14 +75,12 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
ownerRepository.save(owner);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveVisit(Visit visit) throws DataAccessException {
|
||||
visitRepository.save(visit);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Pet findPetById(int id) throws DataAccessException {
|
||||
|
|
@ -101,10 +100,9 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
return vetRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Visit> findVisitsByPetId(int petId) {
|
||||
return visitRepository.findByPetId(petId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Visit> findVisitsByPetId(int petId) {
|
||||
return visitRepository.findByPetId(petId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,18 +23,16 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||
* Controller used to showcase what happens when an exception is thrown
|
||||
*
|
||||
* @author Michael Isvy
|
||||
* <p/>
|
||||
* Also see how the bean of type 'SimpleMappingExceptionResolver' has been declared inside
|
||||
* /WEB-INF/mvc-core-config.xml
|
||||
* <p/>
|
||||
* Also see how a view that resolves to "error" has been added ("error.html").
|
||||
*/
|
||||
@Controller
|
||||
public class CrashController {
|
||||
|
||||
@RequestMapping(value = "/oups", method = RequestMethod.GET)
|
||||
public String triggerException() {
|
||||
throw new RuntimeException("Expected: controller used to showcase what " +
|
||||
"happens when an exception is thrown");
|
||||
throw new RuntimeException(
|
||||
"Expected: controller used to showcase what " + "happens when an exception is thrown");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-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.web;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class CustomErrorController implements ErrorController {
|
||||
|
||||
@RequestMapping(value = "/error")
|
||||
public String error() {
|
||||
return "exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
return "/error";
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.web;
|
|||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.model.Vets;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
|
@ -36,32 +35,28 @@ public class VetController {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public VetController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"/vets.html"})
|
||||
@RequestMapping(value = { "/vets.html" })
|
||||
public String showVetList(Map<String, Object> model) {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet objects
|
||||
// so it is simpler for Object-Xml mapping
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||
// objects so it is simpler for Object-Xml mapping
|
||||
Vets vets = new Vets();
|
||||
vets.getVetList().addAll(this.clinicService.findVets());
|
||||
model.put("vets", vets);
|
||||
return "vets/vetList";
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"/vets.json", "/vets.xml"})
|
||||
public
|
||||
@ResponseBody
|
||||
Vets showResourcesVetList() {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet objects
|
||||
// so it is simpler for JSon/Object mapping
|
||||
@RequestMapping(value = { "/vets.json", "/vets.xml" })
|
||||
public @ResponseBody Vets showResourcesVetList() {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||
// objects so it is simpler for JSon/Object mapping
|
||||
Vets vets = new Vets();
|
||||
vets.getVetList().addAll(this.clinicService.findVets());
|
||||
return vets;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,10 +89,4 @@ public class VisitController {
|
|||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET)
|
||||
public String showVisits(@PathVariable int petId, Map<String, Object> model) {
|
||||
model.put("visits", this.clinicService.findPetById(petId).getVisits());
|
||||
return "visitList";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue