Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RequestMapping(value = { "" }, method = RequestMethod.GET)
- public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
- List<Products> products = productService.findAllCategories();
- int count = products.size(); // also want to include this as count like below response
- return new ResponseEntity<List<Products>>(products, HttpStatus.OK);
- }
- {
- "count": 23,
- "products":[
- {..},
- {..}
- ]
- }
- class ProductsDTO {
- private int count;
- private List<Products> products;
- /* create setters and getters for the fields */
- }
- @RequestMapping(value = { "" }, method = RequestMethod.GET)
- public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
- List<Products> products = productService.findAllCategories();
- int count = products.size();
- ProductsDTO productsDTO = new ProductsDTO();
- productsDTO.setCount(count);
- productsDTO.setProducts(products);
- return new ResponseEntity<ProductsDTO>(productsDTO, HttpStatus.OK);
- }
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.8.5</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.8.5</version>
- </dependency>
- @RequestMapping(value = "/locations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<?> getAll() {
- List<Location> locations = locationService.getLocations();
- List<Country> countries = countryService.getCountries();
- Map<String, Object> result = new HashMap<String,Object>();
- result.put("count",locations.size());
- result.put("locations",locations);
- result.put("countries",countries);
- // Add any additional props that you want to add
- return new ResponseEntity<Map<String,Object>>(result, HttpStatus.OK);
- }
- curl -H "Accept: application/json" -H "Content-type: application/json" -X GET http://localhost:8080/api/locations
- @RequestMapping(value = { "/{productId}", "" }, method = RequestMethod.GET)
- public ResponseEntity<?> greet(@PathVariable Optional<Integer> productId) {
- List<Products> products = productService.findAllCategories();
- HashMap<String, Object> hmap = new HashMap<String, Object>();
- hmap.put("count", products.size());
- hmap.put("products", products);
- // Now I can put as many properties as I want
- return new ResponseEntity<HashMap<String, Object>>(hmap, HttpStatus.OK);
- }
- @RestController
- @RequestMapping("/api")
- public class RestApiController {
- @Autowired
- RepoCity repoCity;
- @Autowired
- RepoCountry repoCountry;
- @Autowired
- RepoLanguage repoLanguage;
- @GetMapping("/allTotal")
- public Map actionAllTotal() {
- Map map = new HashMap();
- map.put("repoCity",repoCity.count());
- map.put("repoCountry",repoCountry.count());
- map.put("repoLanguage",repoLanguage.count());
- Map result = new HashMap();;
- result.put("result",map);
- return result;
- }
- @GetMapping("/country-detail/{id}")
- public Map actionCountryDetail(@PathVariable("id") String id) {
- Map result = new HashMap();
- result.put("result",repoCountry.findOne(id));
- return result;
- }
- }
Add Comment
Please, Sign In to add comment