Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 一次获取多个参数的映射_Spring_Rest_Endpoint_Gateway - Fatal编程技术网

Spring 一次获取多个参数的映射

Spring 一次获取多个参数的映射,spring,rest,endpoint,gateway,Spring,Rest,Endpoint,Gateway,我得到了如下映射: @GetMapping(value = "/topics", params = "dateRange") public Set<Integer> getTopicsInRange(@RequestParam DateRange dateRange) { return topicService.getTopicsInRange(dateRange); } 预期状态代码与实际状态代码不匹配。 我知道,如果我将DateRange参数更改为两个单独的参数,我的解

我得到了如下映射:

@GetMapping(value = "/topics", params = "dateRange")
public Set<Integer> getTopicsInRange(@RequestParam DateRange dateRange) {
    return topicService.getTopicsInRange(dateRange);
}
预期状态代码与实际状态代码不匹配。

我知道,如果我将DateRange参数更改为两个单独的参数,我的解决方案就会起作用,如下所示:

@GetMapping(value = "/topics", params = {"begin", "end"})
public Set<Integer> getTopicsInRange(@RequestParam Date begin, @RequestParam Date end) {
    return topicService.getTopicsInRange(begin, end);
}

添加
DateTimeFormat
注释:

public class DateRange {

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate begin;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate end;

    // getter and setters
}
控制器中的接收对象:

@GetMapping(value = "/topics")
public Set<Integer> getTopicsInRange(DateRange dateRange) {
    return topicService.getTopicsInRange(dateRange);
}

您需要一种方法将DateRange类转换为String,反之亦然。因为您使用的是Spring,所以可以这样做:

@GetMapping(value = "/topics", params = "dateRange")
public Set<Integer> getTopicsInRange(@RequestParam DateRange dateRange) {
    return topicService.getTopicsInRange(dateRange);
}
1)添加转换逻辑

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateRange {

    LocalDate begin;
    LocalDate end;

    private static final String DELIMITER = "_";

    public static DateRange fromFormattedString(String source) {
        if (source != null) {
            String[] tokens = source.split(DELIMITER);
            if (tokens.length == 2) {
                return new DateRange(
                        LocalDate.parse(tokens[0]), // begin
                        LocalDate.parse(tokens[1])  // end
                );
            }
        }
        return null;
    }

    public String toFormattedString() {
        return begin + DELIMITER + end;
    }
}
2)创建弹簧转换器

import org.springframework.core.convert.converter.Converter;

public class DateRangeConverter implements Converter<String, DateRange> {

    @Override
    public DateRange convert(String source) {
        return DateRange.fromFormattedString(source);
    }
}
最后使用类似的:

.get(String.format("/topics?dateRange=%s", VALID_DATE_RANGE.toFormattedString())
或(使用原始字符串):

但是:

尽管如此,我还是建议您使用单独的请求参数(begin、end等),即使有10个,因为它是:

1) 安逸的方式


2) 由于订单请求参数是传入的,因此错误证明并不严格。另一方面,将参数打包到单个对象中会迫使您关注参数的顺序。此外,您必须将所有参数打包为字符串,因此不允许使用可选参数,否则解包逻辑可能会被破坏

什么是
DateRange
?@NickSavenia您在测试中有一个DateRange示例作为有效的\u DATE\u RANGE。DateRange类有两个字段(日期开始和日期结束),可能没有正确的转换
DataRange字符串可用。后数据范围类content@NikolayShevchenko我通过添加DateRange类解决了这个问题,但仍然存在相同的错误400。您的解决方案无效…:(谢谢,我接受了答案,但我能问你一些关于讨论的类似问题吗?
import org.springframework.core.convert.converter.Converter;

public class DateRangeConverter implements Converter<String, DateRange> {

    @Override
    public DateRange convert(String source) {
        return DateRange.fromFormattedString(source);
    }
}
@Configuration
public class WebApiConfiguration extends WebMvcConfigurationSupport {
    ...
    @Override
    public FormattingConversionService mvcConversionService() {
        FormattingConversionService f = super.mvcConversionService();
        f.addConverter(new DateRangeConverter());
        return f;
    }
    ...
}
.get(String.format("/topics?dateRange=%s", VALID_DATE_RANGE.toFormattedString())
.get(String.format("/topics?dateRange=%s", "2018-1-1_2018-2-2")