how to properly validate http request params?

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);
}
}
@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);
}
}
@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
2 Replies
JavaBot
JavaBot14mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot14mo ago
Post Closed
This post has been closed by <@611623200756989972>.

Did you find this page helpful?