Skip to content

Commit b8fc80b

Browse files
committed
Attribute Lazy Loading
1 parent 17a812e commit b8fc80b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
package com.bookstore;
22

3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
58

69
@SpringBootApplication
710
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
817

918
public static void main(String[] args) {
1019
SpringApplication.run(MainApplication.class, args);
1120
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
26+
System.out.println("Persisting several authors ...");
27+
bookstoreService.createAuthors();
28+
};
29+
}
1230
}

HibernateSpringBootAttributeLazyLoadingJacksonSerialization/src/main/java/com/bookstore/controller/BookstoreController.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.core.JsonProcessingException;
66
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
77
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
8-
import java.io.IOException;
98
import java.util.Base64;
109
import java.util.List;
1110
import org.springframework.http.converter.json.MappingJacksonValue;
@@ -27,26 +26,18 @@ public BookstoreController(BookstoreService bookstoreService) {
2726
filterProvider.setFailOnUnknownId(false);
2827
}
2928

30-
@GetMapping("/create")
31-
public String create() throws IOException {
32-
33-
bookstoreService.createAuthors();
34-
35-
return "created";
36-
}
37-
3829
@GetMapping("/author/age/{id}")
3930
public int fetchAuthorAgeViaId(@PathVariable long id) {
40-
return bookstoreService.fetchAuthorAgeViaId(id);
31+
return bookstoreService.fetchAuthorAgeViaId(id);
4132
}
42-
33+
4334
@GetMapping("/author/avatar/{id}")
4435
public String fetchAuthorAvatarViaId(@PathVariable long id) {
45-
return Base64.getEncoder().encodeToString(bookstoreService.fetchAuthorAvatarViaId(id));
36+
return Base64.getEncoder().encodeToString(bookstoreService.fetchAuthorAvatarViaId(id));
4637
}
47-
38+
4839
@GetMapping("/authors/{age}")
49-
public MappingJacksonValue fetchAuthorsByAgeGreaterThanEqual(@PathVariable int age)
40+
public MappingJacksonValue fetchAuthorsByAgeGreaterThanEqual(@PathVariable int age)
5041
throws JsonProcessingException {
5142

5243
List<Author> authors = bookstoreService.fetchAuthorsByAgeGreaterThanEqual(age);
@@ -57,7 +48,7 @@ public MappingJacksonValue fetchAuthorsByAgeGreaterThanEqual(@PathVariable int a
5748
}
5849

5950
@GetMapping("/authors/details/{age}")
60-
public List<Author> fetchAuthorsDetailsByAgeGreaterThanEqual(@PathVariable int age)
51+
public List<Author> fetchAuthorsDetailsByAgeGreaterThanEqual(@PathVariable int age)
6152
throws JsonProcessingException {
6253

6354
return bookstoreService.fetchAuthorsDetailsByAgeGreaterThanEqual(age);

0 commit comments

Comments
 (0)