通过 RestTemplate 发送枚举列表
Sending List of Enum over RestTemplate
我的枚举 Class 是。
public enum TaskName {
VALUE_1("value-1"),
VALUE_2("value-2");
@JsonValue
private String value;
@Override
public String toString() {
return value;
}}
我有一项服务 运行,它使用 RestTemplate.Exchange.
发送 List< TaskName > taskNames
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
);
}
}
还有一个接收器服务,控制器 class 作为。
@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));
}
我得到:
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:
我搜索了很多,但没有得到任何明确的答案。如何将枚举列表发送到接收枚举列表的控制器。
尝试在发送之前将列表转换为数组:
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)));
}
好的,我明白了。我在本地重现了同样的错误。完整错误低于一。
[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]
如果您看到错误,那么您会发现此问题即将到来,因为 HttpMessageConverter 无法将字符串“value-1”转换为相应的枚举值。
出现此错误是因为您重写了枚举中的 toString 方法。由于 toString 方法,当您使用 UriComponentsBuilder 构建查询时,uri 会变成这样。
?taskNames=value-1&&taskNames=value-2
但是转换器不知道 value-1 或 value-2,因为它们只是附加到枚举值的字段的值,而不是实际的枚举值。
如果您删除 toStringMethod
那么 uri 将变成如下所示。
?taskNames=VALUE_1&&taskNames=VALUE_2
然后它会很容易映射到列表;
请试一次
我的枚举 Class 是。
public enum TaskName {
VALUE_1("value-1"),
VALUE_2("value-2");
@JsonValue
private String value;
@Override
public String toString() {
return value;
}}
我有一项服务 运行,它使用 RestTemplate.Exchange.
发送 List< TaskName > taskNamesfinal 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
);
}
}
还有一个接收器服务,控制器 class 作为。
@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));
}
我得到:
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:
我搜索了很多,但没有得到任何明确的答案。如何将枚举列表发送到接收枚举列表的控制器。
尝试在发送之前将列表转换为数组:
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)));
}
好的,我明白了。我在本地重现了同样的错误。完整错误低于一。
[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]
如果您看到错误,那么您会发现此问题即将到来,因为 HttpMessageConverter 无法将字符串“value-1”转换为相应的枚举值。
出现此错误是因为您重写了枚举中的 toString 方法。由于 toString 方法,当您使用 UriComponentsBuilder 构建查询时,uri 会变成这样。
?taskNames=value-1&&taskNames=value-2
但是转换器不知道 value-1 或 value-2,因为它们只是附加到枚举值的字段的值,而不是实际的枚举值。
如果您删除 toStringMethod
那么 uri 将变成如下所示。
?taskNames=VALUE_1&&taskNames=VALUE_2
然后它会很容易映射到列表;
请试一次