【问题标题】:dynamic URI comma separated for request mapping param spring boot用于请求映射参数 spring boot 的动态 URI 逗号分隔
【发布时间】:2020-05-12 22:23:43
【问题描述】:
@DeleteMapping(path = "delete/{settingId:[\\d]+}/{userId:[\\d]+}")
public @ResponseBody ResponseEntity<ResponseDTO<String>> deleteSettingById(
        @PathVariable Integer[] settingId,)

我想像这样在邮递员中测试 http://localhost:9994/setting/delete/54793685,54793685/6 如何更改 settingId 以接受来自角度的逗号分隔 id

【问题讨论】:

    标签: angularjs regex angular spring spring-boot


    【解决方案1】:

    您可以使用自定义反序列化器创建 POJO。

    public class SettingIds {
        private int[] ids;
        // getter,setter,constructor
    }
    

    解串器需要扩展PropertyEditorSupport:

    @Component
    public class SettingIdsDeserializer extends PropertyEditorSupport {
    
      @Override
      public void setAsText(String text) throws IllegalArgumentException {
          String[] splitEntry = text.split(",");
          int[] idArray = Stream.of(splitEntry).mapToInt(Integer::parseInt).toArray();
          setValue(new SettingIds(idArray));
      }
    }
    

    然后你将反序列化器连接到你的控制器并使用 init binder 注解注册它:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(SettingIds.class, settingIdsDeserializer);
    }
    

    现在您的端点可能如下所示:

      @DeleteMapping(path = "delete/{settingIds:[\\d]+}/{userId:[\\d]+}")
      public @ResponseBody ResponseEntity<ResponseDTO<String>> deleteSettingById(
        @PathVariable SettingIds settingIds)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-25
      • 2023-04-07
      • 2020-11-06
      • 2021-12-15
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多