【问题标题】:Sending List of Enum over RestTemplate通过 RestTemplate 发送枚举列表
【发布时间】:2021-09-30 16:45:34
【问题描述】:

我的枚举类是。

public enum TaskName {

VALUE_1("value-1"),
VALUE_2("value-2");

@JsonValue
private String value;

@Override
public String toString() {
    return value;
}}

我有一个正在运行的服务,它使用 RestTemplate.Exchange 发送 List 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      
        );
        }
}

还有一个具有控制器类的接收器服务。

@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:

我搜索了很多,但没有得到任何明确的答案。如何将枚举列表发送到接收枚举列表的控制器。

【问题讨论】:

    标签: java spring resttemplate


    【解决方案1】:

    好的,我知道了。我在本地重现了同样的错误。完整的错误低于 1。

    [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”转换为相应的 Enum 值。

    出现此错误是因为您覆盖了枚举中的 toString 方法。由于使用 UriComponentsBuilder 构建查询时的 toString 方法,因此 uri 变成了这样。
    ?taskNames=value-1&amp;&amp;taskNames=value-2 但转换器不知道 value-1 或 value-2,因为它们只是附加到枚举值的字段的值,而不是实际的枚举值。

    如果您删除 toStringMethod,那么 uri 将变为如下所示。

    ?taskNames=VALUE_1&amp;&amp;taskNames=VALUE_2 然后很容易映射到List;

    请尝试一次。

    【讨论】:

    • 救命稻草!!非常感谢。
    【解决方案2】:

    在发送之前尝试将列表转换为数组:

    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)));
            }
    

    【讨论】:

    • 我无法接收 List 转换为 String[] 数组
    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多