Skip to content

Commit a23563c

Browse files
顾鲍尔顾鲍尔
authored andcommitted
boylegu#13 Resolve due to @Autowired lead to NullPointerException problem
1 parent a0478df commit a23563c

File tree

4 files changed

+184
-45
lines changed

4 files changed

+184
-45
lines changed

src/main/java/com/boylegu/springboot_vue/App.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.boylegu.springboot_vue;
2-
2+
import org.springframework.context.annotation.Configuration;
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66

7+
@Configuration
78
@SpringBootApplication
89
public class App {
910

src/main/java/com/boylegu/springboot_vue/controller/MainController.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.boylegu.springboot_vue.controller.pagination.PaginationMultiTypeValuesHelper;
2020
import com.boylegu.springboot_vue.dao.PersonsRepository;
21+
import com.boylegu.springboot_vue.controller.pagination.PaginationFormatting;
2122

2223
import java.util.*;
2324

@@ -26,18 +27,6 @@
2627
@RequestMapping("/api/persons")
2728
public class MainController {
2829

29-
/**
30-
* @api {GET} /api/persons/sex Get all sexList
31-
* @apiName GetAllSexList
32-
* @apiGroup Info Manage
33-
* @apiVersion 1.0.0
34-
* @apiExample {httpie} Example usage:
35-
* <p>
36-
* http /api/persons/sex
37-
* @apiSuccess {String} label
38-
* @apiSuccess {String} value
39-
*/
40-
4130
@Autowired
4231
private PersonsRepository personsRepository;
4332

@@ -47,6 +36,18 @@ public class MainController {
4736
@RequestMapping(value = "/sex", method = RequestMethod.GET)
4837
public ResponseEntity<?> getSexAll() {
4938

39+
/*
40+
* @api {GET} /api/persons/sex Get all sexList
41+
* @apiName GetAllSexList
42+
* @apiGroup Info Manage
43+
* @apiVersion 1.0.0
44+
* @apiExample {httpie} Example usage:
45+
* <p>
46+
* http /api/persons/sex
47+
* @apiSuccess {String} label
48+
* @apiSuccess {String} value
49+
*/
50+
5051
ArrayList<Map<String, String>> results = new ArrayList<>();
5152

5253
for (Object value : personsRepository.findSex()) {
@@ -62,7 +63,6 @@ public ResponseEntity<?> getSexAll() {
6263
}
6364

6465
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
65-
//public Map<String, PaginationMultiTypeValuesHelper> getPersonsAll
6666
public Map<String, PaginationMultiTypeValuesHelper> getPersonsAll(
6767
@RequestParam(value = "page", required = false) Integer pages,
6868
@RequestParam("sex") String sex,
@@ -79,36 +79,35 @@ public Map<String, PaginationMultiTypeValuesHelper> getPersonsAll(
7979
PaginationMultiTypeValuesHelper multiValue = new PaginationMultiTypeValuesHelper();
8080
Map<String, PaginationMultiTypeValuesHelper> results = new HashMap<>();
8181

82-
Integer count, page_number;
83-
Object content;
84-
Long total;
85-
if (sex.length() == 0 && email.length() == 0) {
86-
count = personsRepository.findAll(pageable).getSize();
87-
page_number = personsRepository.findAll(pageable).getNumber();
88-
content = personsRepository.findAll(pageable).getContent();
89-
total = personsRepository.findAll(pageable).getTotalElements();
90-
91-
} else if (sex.length() > 0 && email.length() > 0) {
92-
count = personsRepository.findBySexAndEmailContains(sex, email, pageable).getSize();
93-
page_number = personsRepository.findBySexAndEmailContains(sex, email, pageable).getNumber();
94-
content = personsRepository.findBySexAndEmailContains(sex, email, pageable).getContent();
95-
total = personsRepository.findBySexAndEmailContains(sex, email, pageable).getTotalElements();
96-
97-
} else {
98-
count = personsRepository.findBySex(sex, pageable).getSize();
99-
page_number = personsRepository.findBySex(sex, pageable).getNumber();
100-
content = personsRepository.findBySex(sex, pageable).getContent();
101-
total = personsRepository.findBySex(sex, pageable).getTotalElements();
102-
103-
}
104-
105-
multiValue.setCount(count);
106-
multiValue.setPage(page_number + 1);
107-
multiValue.setResults(content);
108-
multiValue.setTotal(total);
109-
results.put("data", multiValue);
110-
111-
return results;
82+
// Integer count, page_number;
83+
// Object content;
84+
// Long total;
85+
// if (sex.length() == 0 && email.length() == 0) {
86+
// count = personsRepository.findAll(pageable).getSize();
87+
// page_number = personsRepository.findAll(pageable).getNumber();
88+
// content = personsRepository.findAll(pageable).getContent();
89+
// total = personsRepository.findAll(pageable).getTotalElements();
90+
// } else if (sex.length() > 0 && email.length() > 0) {
91+
// count = personsRepository.findBySexAndEmailContains(sex, email, pageable).getSize();
92+
// page_number = personsRepository.findBySexAndEmailContains(sex, email, pageable).getNumber();
93+
// content = personsRepository.findBySexAndEmailContains(sex, email, pageable).getContent();
94+
// total = personsRepository.findBySexAndEmailContains(sex, email, pageable).getTotalElements();
95+
// } else {
96+
// count = personsRepository.findBySex(sex, pageable).getSize();
97+
// page_number = personsRepository.findBySex(sex, pageable).getNumber();
98+
// content = personsRepository.findBySex(sex, pageable).getContent();
99+
// total = personsRepository.findBySex(sex, pageable).getTotalElements();
100+
// }
101+
PaginationFormatting ss = new PaginationFormatting();
102+
103+
104+
// multiValue.setCount(count);
105+
// multiValue.setPage(page_number + 1);
106+
// multiValue.setResults(content);
107+
// multiValue.setTotal(total);
108+
// results.put("data", multiValue);
109+
110+
return ss.filterQuery(sex, email, pageable);
112111
}
113112

114113
}
Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,143 @@
11
package com.boylegu.springboot_vue.controller.pagination;
22

3-
public class PaginationFormatting {
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.domain.Page;
9+
import org.springframework.beans.BeansException;
10+
import org.springframework.context.ApplicationContext;
11+
import org.springframework.context.ApplicationContextAware;
12+
13+
import com.boylegu.springboot_vue.dao.PersonsRepository;
14+
import com.boylegu.springboot_vue.entities.Persons;
15+
16+
17+
@Component
18+
class SpringUtil implements ApplicationContextAware {
19+
private static ApplicationContext applicationContext = null;
20+
21+
@Override
22+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
23+
if (SpringUtil.applicationContext == null) {
24+
SpringUtil.applicationContext = applicationContext;
25+
}
26+
}
27+
28+
public static ApplicationContext getApplicationContext() {
29+
return applicationContext;
30+
}
31+
32+
public static Object getBean(String name) {
33+
return getApplicationContext().getBean(name);
34+
}
35+
36+
public static <T> T getBean(Class<T> clazz) {
37+
return getApplicationContext().getBean(clazz);
38+
}
39+
40+
public static <T> T getBean(String name, Class<T> clazz) {
41+
return getApplicationContext().getBean(name, clazz);
42+
}
43+
}
44+
45+
46+
interface Types {
47+
public Page<Persons> query();
48+
49+
public Integer getCount();
50+
51+
public Integer getPageNumber();
52+
53+
public Long getTotal();
54+
55+
public Object getContent();
56+
}
57+
58+
class AllType implements Types {
59+
60+
61+
private Pageable pageable;
62+
63+
public String sex, email;
464

565

66+
public AllType(Pageable pageable) { //String sexName, String emailName,
67+
// super(sexName, emailName, pageable);
68+
this.pageable = pageable;
69+
}
70+
71+
public Page<Persons> query() {
72+
73+
System.out.println(SpringUtil.getBean(PersonsRepository.class).findAll());
74+
return SpringUtil.getBean(PersonsRepository.class).findAll(
75+
this.pageable
76+
);
77+
}
78+
79+
public Integer getCount() {
80+
return this.query().getSize();
81+
}
82+
83+
public Integer getPageNumber() {
84+
return this.query().getNumber();
85+
86+
}
87+
88+
public Long getTotal() {
89+
return this.query().getTotalElements();
90+
}
91+
92+
public Object getContent() {
93+
return this.query().getContent();
94+
}
695
}
96+
97+
// class SexType extends BasePaginationInfo implements Types {
98+
// public Page<Persons> query(String sexName, String emailName) {
99+
// return personsRepository.findBySex(
100+
// sexName,
101+
// pageable
102+
// );
103+
// }
104+
// }
105+
//
106+
// class SexMailType extends BasePaginationInfo implements Types {
107+
// public Page<Persons> query(String sexName, String emailName) {
108+
// return personsRepository.findBySexAndEmailContains(
109+
// sexName,
110+
// emailName,
111+
// pageable
112+
// );
113+
// }
114+
// }
115+
116+
117+
public class PaginationFormatting {
118+
119+
private PaginationMultiTypeValuesHelper multiValue = new PaginationMultiTypeValuesHelper();
120+
121+
private Map<String, PaginationMultiTypeValuesHelper> results = new HashMap<>();
122+
123+
public Map<String, PaginationMultiTypeValuesHelper> filterQuery(String sex, String email, Pageable pageable) {
124+
125+
if (sex.length() == 0 && email.length() == 0) {
126+
// Types typeInstance = new AllType(sex, email, pageable);
127+
Types typeInstance = new AllType(pageable);
128+
this.multiValue.setCount(typeInstance.getCount());
129+
130+
this.multiValue.setPage(typeInstance.getPageNumber() + 1);
131+
132+
this.multiValue.setResults(typeInstance.getContent());
133+
134+
this.multiValue.setTotal(typeInstance.getTotal());
135+
136+
this.results.put("data", this.multiValue);
137+
138+
}
139+
140+
return results;
141+
}
142+
143+
}

src/main/java/com/boylegu/springboot_vue/dao/PersonsRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import org.springframework.data.jpa.repository.Query;
55
import org.springframework.data.domain.Page;
66
import org.springframework.data.domain.Pageable;
7+
import org.springframework.stereotype.Repository;
78

89
import com.boylegu.springboot_vue.entities.Persons;
10+
import org.springframework.web.bind.annotation.RequestParam;
911

1012
import java.util.List;
1113

0 commit comments

Comments
 (0)