hey guys. i have my controller:
@RestController
@RequestMapping("api/v1/")
@Validated
public class MainController {
private final ItemService itemService;
public MainController(ItemService itemService) {
this.itemService = itemService;
}
@GetMapping()
@ResponseBody
public ResponseEntity<List<Item>> getItems(@RequestParam("distance") double distance, @RequestParam("season") String season) {
System.out.println(distance);
System.out.println(season);
List<Item> listOfItems=itemService.getItemsByDistanceAndSeason(distance, season);
return ResponseEntity.ok(listOfItems);
}
}
and my service:
@Service
public class ItemService {
private ItemRepository itemRepository;
public ItemService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getItemsByDistanceAndSeason(double distance, String season) {
if (distance<=0) {
System.out.println("Distance should be more than 0");
//todo handle. add validation
throw new ArithmeticException("Distance should be more than 0");
}
if (season.equals("all") || season.equals("winter") || season.equals("spring") || season.equals("summer") || season.equals("autumn")) {
}
return itemRepository.getAllByDistanceAndSeason(distance, season);
}
}
and i was wondering how to validate my path variables. the thing is that my service just calls the repo class, it calls the db. and the DB just returns an entity Item. so idk how to validate my request params. should i just add the validation annotations? but idk where to add them. i tried googling java spring request param validation but got no informative results. can smb help me out? thx