Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8d6f4e

Browse files
Step 6. Create new REST controller
1 parent44ab44d commitb8d6f4e

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

‎existing-spring-boot-app-modification/spring-petclinic-main/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies {
3838
implementation'org.springframework.boot:spring-boot-starter-security'
3939
compileOnly'org.mapstruct:mapstruct:1.5.3.Final'
4040
annotationProcessor'org.mapstruct:mapstruct-processor:1.5.3.Final'
41+
testImplementation'org.springframework.security:spring-security-test'
4142
}
4243

4344
tasks.named('test') {

‎existing-spring-boot-app-modification/spring-petclinic-main/src/main/java/org/springframework/samples/petclinic/WebSecurityConfiguration.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2323
http.authorizeHttpRequests(authorizeHttpRequests ->authorizeHttpRequests
2424
.requestMatchers(HttpMethod.GET,"/vets")
2525
.permitAll()
26+
.requestMatchers("/rest/vets/**")
27+
.permitAll()
2628
.anyRequest()
2729
.authenticated());
2830
http.headers(Customizer.withDefaults());

‎existing-spring-boot-app-modification/spring-petclinic-main/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
importorg.springframework.transaction.annotation.Transactional;
2424

2525
importjava.util.Collection;
26+
importjava.util.List;
2627

2728
/**
2829
* Repository class for <code>Vet</code> domain objects All method names are compliant
@@ -55,4 +56,5 @@ public interface VetRepository extends Repository<Vet, Integer> {
5556
@Cacheable("vets")
5657
Page<Vet>findAll(Pageablepageable)throwsDataAccessException;
5758

59+
List<Vet>findBySpecialtiesIn(Collection<Specialty>specialties);
5860
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packageorg.springframework.samples.petclinic.vet;
2+
3+
importorg.springframework.dao.DataAccessException;
4+
importorg.springframework.web.bind.annotation.GetMapping;
5+
importorg.springframework.web.bind.annotation.RequestMapping;
6+
importorg.springframework.web.bind.annotation.RequestParam;
7+
importorg.springframework.web.bind.annotation.RestController;
8+
9+
importjava.util.Collection;
10+
importjava.util.List;
11+
12+
@RestController
13+
@RequestMapping("/rest/vets")
14+
publicclassVetRestController {
15+
16+
privatefinalVetRepositoryvetRepository;
17+
18+
privatefinalVetMappervetMapper;
19+
20+
privatefinalSpecialtyRepositoryspecialtyRepository;
21+
22+
publicVetRestController(VetRepositoryvetRepository,
23+
VetMappervetMapper,
24+
SpecialtyRepositoryspecialtyRepository) {
25+
this.vetRepository =vetRepository;
26+
this.vetMapper =vetMapper;
27+
this.specialtyRepository =specialtyRepository;
28+
}
29+
30+
@GetMapping
31+
publicCollection<VetWithoutSalaryDto>findAll()throwsDataAccessException {
32+
Collection<Vet>vets =vetRepository.findAll();
33+
Collection<VetWithoutSalaryDto>vetWithoutSalaryDtos =vets.stream()
34+
.map(vetMapper::toDto)
35+
.toList();
36+
returnvetWithoutSalaryDtos;
37+
}
38+
39+
@GetMapping(path = {"/by-specialty"})
40+
publicList<VetWithoutSalaryDto>findBySpecialtiesIn(@RequestParamCollection<Integer>specialtyIds) {
41+
Collection<Specialty>specialties =specialtyIds.stream()
42+
.map(specialtyRepository::getReferenceById)
43+
.toList();
44+
List<Vet>vets =vetRepository.findBySpecialtiesIn(specialties);
45+
List<VetWithoutSalaryDto>vetWithoutSalaryDtos =vets.stream()
46+
.map(vetMapper::toDto)
47+
.toList();
48+
returnvetWithoutSalaryDtos;
49+
}
50+
}
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
packageorg.springframework.samples.petclinic.vet;
2+
3+
importorg.junit.jupiter.api.BeforeEach;
4+
importorg.junit.jupiter.api.DisplayName;
5+
importorg.junit.jupiter.api.Test;
6+
importorg.springframework.beans.factory.annotation.Autowired;
7+
importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
importorg.springframework.boot.test.context.SpringBootTest;
9+
importorg.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
10+
importorg.springframework.test.web.servlet.MockMvc;
11+
12+
importstaticorg.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
13+
importstaticorg.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
14+
importstaticorg.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
15+
16+
/**
17+
* Test class for the {@link VetRestController}
18+
*/
19+
@SpringBootTest
20+
@AutoConfigureMockMvc
21+
publicclassVetRestControllerTest {
22+
23+
@Autowired
24+
privateMockMvcmockMvc;
25+
26+
@BeforeEach
27+
publicvoidsetup() {
28+
29+
}
30+
31+
@Test
32+
@DisplayName("Test find all")
33+
publicvoidfindAllTest()throwsException {
34+
mockMvc.perform(get("/rest/vets")
35+
.with(SecurityMockMvcRequestPostProcessors.user("user")))
36+
.andExpect(status()
37+
.isOk())
38+
.andDo(print());
39+
}
40+
41+
@Test
42+
@DisplayName("Test find by specialties in")
43+
publicvoidfindBySpecialtiesInTest()throwsException {
44+
StringspecialtyIds ="1,2";
45+
46+
mockMvc.perform(get("/rest/vets/by-specialty")
47+
.param("specialtyIds",specialtyIds)
48+
.with(SecurityMockMvcRequestPostProcessors.user("user")))
49+
.andExpect(status()
50+
.isOk())
51+
.andDo(print());
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp