My enum Class is.
public enum TaskName {
VALUE_1("value-1"),
VALUE_2("value-2");
@JsonValue
private String value;
@Override
public String toString() {
return value;
}}
I have a service running which send the List< TaskName > taskNames using RestTemplate.Exchange.
final String uriWithPathParms = String.format("%s/somePath/%s", first, second);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriWithPathParms)
.queryParam("status", "OK")
.queryParam("DATE", SomeDate)
.queryParam("taskNames", taskNames);
final String uriWithQueryParms = builder.toUriString();
HttpEntity<> httpEntity = new HttpEntity<>((prepareHttpHeaders(uriWithQueryParms, customHttpHeaders)));
try {
ResponseEntity<List<SomeClass>> responseEntity = restTemplate.exchange(
uriWithQueryParms,
HttpMethod.GET,
httpEntity
);
}
}
And also a reciever services having the controller class as.
@GetMapping(value = "/first/somePath/second")
public HttpEntity<List<SomeClass>> demoController(
@RequestParam(value = "status", required = true) StatusEnum status,
@RequestParam(value = "DATE", required = true) LocalDateTime date,
@RequestParam(value = "taskNames") List<TaskName> taskNames) {
return ResponseEntity.ok(someService.someMethod(, status, date, taskNames));
}
I am getting:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List'; nested exception is org.springframework.core.convert.ConversionFailedException:
I search alot but didn't get any clear answer. How can I send List of Enums to a controller which recieves List of Enums.
CodePudding user response:
Try converting the list to an array before sending :
public HttpEntity<List<SomeClass>> demoController(
@RequestParam(value = "status", required = true) StatusEnum status,
@RequestParam(value = "DATE", required = true) LocalDateTime date,
@RequestParam(value = "taskNames") List<TaskName> taskNames) {
return ResponseEntity.ok(someService.someMethod(, status, date, taskNames.stream().toArray(String[]::new)));
}
CodePudding user response:
Ok I Got the issue. I have reproduced the same error in my local. The full error is below one.
[org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam com.kapil.SpringJPA.Entity.TaskName] for value 'value-1'; nested exception is java.lang.IllegalArgumentException: No enum constant com.kapil.SpringJPA.Entity.TaskName.value-1]
If you see the error then you will find that this issue is coming because HttpMessageConverter is not able to convert the string " value-1 " to corresponding Enum value.
This error is coming because you have overridden the toString method in enum. Because of toString method when your query is building using UriComponentsBuilder then uri becomes something like this.
?taskNames=value-1&&taskNames=value-2
but converter don't know about value-1 or value-2 as these are just a value for the field that is attached to enum value and Not the actual enum value.
If you remove toStringMethod
then uri will become some like below.
?taskNames=VALUE_1&&taskNames=VALUE_2
Then it will easily mapped to List;
Please try once.