Skip to content

Commit 2d6c783

Browse files
authored
Merge pull request boylegu#9 from boylegu/issue/8/response_format
close boylegu#8 finished sex api and override it response formatting
2 parents d81c7d9 + dc58d41 commit 2d6c783

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

info.db

0 Bytes
Binary file not shown.
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
package com.boylegu.springboot_vue.controller;
22

33
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
45
import org.springframework.web.bind.annotation.RestController;
56
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.http.HttpStatus;
79

810
import com.boylegu.springboot_vue.dao.PersonsRepository;
9-
import com.boylegu.springboot_vue.entities.Persons;
1011

12+
import java.util.Map;
13+
import java.util.HashMap;
14+
import java.util.ArrayList;
1115

12-
import java.util.List;
1316

1417
@RestController
1518
@RequestMapping("/api/persons")
1619
public class MainController {
1720

21+
/**
22+
* @api {GET} /api/persons/sex Get all sexList
23+
* @apiName GetAllSexList
24+
* @apiGroup Info Manage
25+
* @apiVersion 1.0.0
26+
* @apiExample {httpie} Example usage:
27+
* <p>
28+
* http /api/persons/sex
29+
* @apiSuccess {String} label
30+
* @apiSuccess {String} value
31+
*/
32+
1833
@Autowired
1934
private PersonsRepository personsRepository;
2035

21-
@RequestMapping(value="/sex", method = RequestMethod.GET)
22-
public List<Persons> getAll() {
36+
@RequestMapping(value = "/sex", method = RequestMethod.GET)
37+
public ResponseEntity<?> getAll() {
2338

24-
return personsRepository.findAll();
39+
ArrayList<Map<String, String>> results = new ArrayList<Map<java.lang.String, java.lang.String>>();
40+
41+
for (Object value : personsRepository.findSex()) {
42+
Map<String, String> sex = new HashMap<String, String>();
43+
44+
sex.put("label", value.toString());
45+
sex.put("value", value.toString());
46+
results.add(sex);
47+
}
48+
ResponseEntity<ArrayList<Map<String, String>>> responseEntity = new ResponseEntity<>(results,
49+
HttpStatus.OK);
50+
return responseEntity;
2551
}
52+
2653
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.boylegu.springboot_vue.dao;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
45

56
import com.boylegu.springboot_vue.entities.Persons;
67

8+
import java.util.List;
9+
710

811
public interface PersonsRepository extends JpaRepository<Persons, Long> {
12+
13+
public static final String FIND_SEX = "select DISTINCT sex from Persons p";
14+
15+
@Query(FIND_SEX)
16+
List<Persons> findSex();
917
}

0 commit comments

Comments
 (0)