Guest User

Untitled

a guest
Nov 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. @RequestMapping(value = { "" }, method = RequestMethod.GET)
  2. public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
  3.  
  4. List<Products> products = productService.findAllCategories();
  5. int count = products.size(); // also want to include this as count like below response
  6. return new ResponseEntity<List<Products>>(products, HttpStatus.OK);
  7. }
  8.  
  9. {
  10. "count": 23,
  11. "products":[
  12. {..},
  13. {..}
  14. ]
  15. }
  16.  
  17. class ProductsDTO {
  18.  
  19. private int count;
  20. private List<Products> products;
  21.  
  22. /* create setters and getters for the fields */
  23.  
  24. }
  25.  
  26. @RequestMapping(value = { "" }, method = RequestMethod.GET)
  27. public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
  28.  
  29. List<Products> products = productService.findAllCategories();
  30. int count = products.size();
  31. ProductsDTO productsDTO = new ProductsDTO();
  32. productsDTO.setCount(count);
  33. productsDTO.setProducts(products);
  34. return new ResponseEntity<ProductsDTO>(productsDTO, HttpStatus.OK);
  35. }
  36.  
  37. <dependency>
  38. <groupId>com.fasterxml.jackson.core</groupId>
  39. <artifactId>jackson-core</artifactId>
  40. <version>2.8.5</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.fasterxml.jackson.core</groupId>
  44. <artifactId>jackson-databind</artifactId>
  45. <version>2.8.5</version>
  46. </dependency>
  47.  
  48. @RequestMapping(value = "/locations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  49. public ResponseEntity<?> getAll() {
  50. List<Location> locations = locationService.getLocations();
  51. List<Country> countries = countryService.getCountries();
  52.  
  53. Map<String, Object> result = new HashMap<String,Object>();
  54. result.put("count",locations.size());
  55. result.put("locations",locations);
  56. result.put("countries",countries);
  57. // Add any additional props that you want to add
  58. return new ResponseEntity<Map<String,Object>>(result, HttpStatus.OK);
  59. }
  60.  
  61. curl -H "Accept: application/json" -H "Content-type: application/json" -X GET http://localhost:8080/api/locations
  62.  
  63. @RequestMapping(value = { "/{productId}", "" }, method = RequestMethod.GET)
  64. public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
  65. List<Products> products = productService.findAllCategories();
  66.  
  67. HashMap<String, Object> hmap = new HashMap<String, Object>();
  68. hmap.put("count", products.size());
  69. hmap.put("products", products);
  70. // Now I can put as many properties as I want
  71.  
  72. return new ResponseEntity<HashMap<String, Object>>(hmap, HttpStatus.OK);
  73. }
  74.  
  75. @RestController
  76. @RequestMapping("/api")
  77. public class RestApiController {
  78. @Autowired
  79. RepoCity repoCity;
  80.  
  81. @Autowired
  82. RepoCountry repoCountry;
  83.  
  84. @Autowired
  85. RepoLanguage repoLanguage;
  86.  
  87. @GetMapping("/allTotal")
  88. public Map actionAllTotal() {
  89. Map map = new HashMap();
  90. map.put("repoCity",repoCity.count());
  91. map.put("repoCountry",repoCountry.count());
  92. map.put("repoLanguage",repoLanguage.count());
  93. Map result = new HashMap();;
  94. result.put("result",map);
  95. return result;
  96. }
  97.  
  98. @GetMapping("/country-detail/{id}")
  99. public Map actionCountryDetail(@PathVariable("id") String id) {
  100. Map result = new HashMap();
  101. result.put("result",repoCountry.findOne(id));
  102. return result;
  103. }
  104. }
Add Comment
Please, Sign In to add comment