I am creating a method for an API. In this method i have some parameters that are optional. those are filters for searching an event. when i try to run it and type the following url:
http://localhost:8181/api/events?id=gRDHzDh9TdiLDAZgrZc2wg==
i get this error message:
Failed to convert value of type 'java.lang.String' to required type 'java.util.UUID'; nested exception is java.lang.IllegalArgumentException: Invalid UUID string: gRDHzDh9TdiLDAZgrZc2wg==
So i understand that i insert a String in my url and expect a UUID in code, but how do i convert this? Below here is my code:
@RequestMapping(
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity getEvents(@RequestParam(value = "id", required = false) UUID eventId,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "playtype", required = false) PlayType playType,
@RequestParam(value = "skilllevel", required = false) SkillLevel skillLevel,
@RequestParam(value = "sporttype", required = false) SportType sportType,
@RequestParam(value = "long", required = false) String _long,
@RequestParam(value = "lat", required = false) String lat) {
try {
List<Event> events = eventService.getEvents(eventId, title, playType, skillLevel, sportType, _long, lat);
if (events.size() == 0) {
return new ResponseEntity("No events found", HttpStatus.OK);
}
return new ResponseEntity(events, HttpStatus.OK);
} catch (Exception ex){
return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
So here are my 2 questions:
How do i convert the string to a valid UUID input in the RequestParam?
How do i convert the string to a valid enum in the RequestParam? (because with the enums i have the same error)
EDIT
my code is now like this:
@RequestMapping(
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity getEvents(@RequestParam(value = "id", required = false) String eventId,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "playtype", required = false) String playType,
@RequestParam(value = "skilllevel", required = false) String skillLevel,
@RequestParam(value = "sporttype", required = false) String sportType,
@RequestParam(value = "long", required = false) String _long,
@RequestParam(value = "lat", required = false) String lat) {
UUID id = null;
PlayType playType1 = null;
SkillLevel skillLevel1 = null;
SportType sportType1 = null;
try {
if (eventId != null){
id = UUID.fromString(eventId);
}
if (playType != null){
playType1 = PlayType.valueOf(playType);
}
if (skillLevel != null){
skillLevel1 = SkillLevel.valueOf(skillLevel);
}
if (sportType != null){
sportType1 = SportType.valueOf(sportType);
}
List<Event> events = eventService.getEvents(id, title, playType1, skillLevel1, sportType1, _long, lat);
if (events.size() == 0) {
return new ResponseEntity("No events found", HttpStatus.OK);
}
return new ResponseEntity(events, HttpStatus.OK);
} catch (Exception ex){
return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
but i still get an error:
Invalid UUID string: gRDHzDh9TdiLDAZgrZc2wg==
You need to use
UUID.fromString()
API, see here
You need to use
Enum.valueOf()
API, see here