1 Star 1 Fork 2

火星人 / spring-dynamic-params

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

spring-dynamic-params

spring boot mvc参数类型转换,支持转换为子类对象访问以及参数验证

使用示例:

添加依赖

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
}	
dependencies {
        // 低版本gradle使用
        // compile 'com.gitee.luws:spring-dynamic-params:Tag'
        // 版本号查看仓库tag
        implementation 'com.gitee.luws:spring-dynamic-params:0.1.3'
        // 也可以使用GitHub
        // implementation 'com.github.shenluw:spring-dynamic-params:0.1.3'
}

方法一

  • 对实体对象添加@Sldp注解
@Sldp
public class Animal {
    private String name;
    private int age;
    ...
}
public class Dog extends Animal {
    private String face;
    private String speed;
    ...
}
  • 创建添加访问接口
public class TestController {
    private final static Logger log = getLogger(TestController.class);

    @RequestMapping("/1")
    public void test1(Animal a) {
        log.info("test1: {}", a);
    }
}
  • 测试接口

request:

http://xxxxx/1?name=test&age=12&speed=120&sldp=top.shenluw.sldp.Dog

response:

a 实际类型为 Dog

方法二

为方法添加@Sldp注解 删除Animal上的注解

public class Animal {
    private String name;
    private int age;
    ...
}
  • 修改访问接口
public class TestController {
    private final static Logger log = getLogger(TestController.class);

    @RequestMapping("/1")
    public void test1(@Sldp Animal a) {
        log.info("test1: {}", a);
    }
}
  • 测试接口

request:

http://xxxxx/1?name=test&age=12&speed=120&sldp=top.shenluw.sldp.Dog

response:

a 实际类型为 Dog

方法三

实体对象实现DynamicModel接口 删除Animal上的注解

public class Animal implements DynamicModel {
    private String name;
    private int age;
    ...
}
  • 修改访问接口
public class TestController {
    private final static Logger log = getLogger(TestController.class);

    @RequestMapping("/1")
    public void test1(Animal a) {
        log.info("test1: {}", a);
    }
}
  • 测试接口

request:

http://xxxxx/1?name=test&age=12&speed=120&sldp=top.shenluw.sldp.Dog

response:

a 实际类型为 Dog

使用json对象请求

接口可以实现参数为json格式的请求并转换为对应的格式

修改访问接口,设置注解参数为Json类型即可支持

@RestController
public class TestController {
    private final static Logger log = getLogger(TestController.class);

    @RequestMapping("/1")
    public void test1(@Sldp(type = ModelType.Json) Animal a) {
        log.info("test1: {}", a);
    }
    
}

修改请求参数,添加json数据对应的字段即可实现

http://xxxxx/1?sldpJson={"name":"test name","age":12}&sldp=top.shenluw.sldp.Dog

支持多个对象参数

修改访问接口,设置注解参数别名即可支持

@RestController
public class TestController {
    private final static Logger log = getLogger(TestController.class);

    @RequestMapping("/1")
    public void test1(@Sldp(name = "a") Animal a, @Sldp(name = "b") Animal b) {
        log.info("test1: {} {}", a, b);
    }
    
}

修改请求参数,为不同别名设置不通类型

http://xxxxx/1?name=test&age=12&face=mm&speed=1233&height=11&a=top.shenluw.sldp.BDog&b=top.shenluw.sldp.Cat

为类型设置别名

修改yaml,将top.shenluw.sldp.Dog别名修改为myName

sldp:
  # 为类型设置别名
  type-alias:
      myName: top.shenluw.sldp.Dog

把请求参数中的sldp值修改为myName即可实现

http://xxxxx/1?sldpJson={"name":"test name","age":12}&sldp=myName

添加加密设置

sldp:
  # 开启加密,默认关闭
  enable-security: true
  # 开启加密后是否使用全局加密,默认只对设置了SlSecurity注解的方法使用加密
  default-security: true

当前加密只支持json方式,默认只提供了base64的测试样例

添加新的加密方法只需要实现Encryptor接口,然后配置成bean既可以

@Bean
public Encryptor myEncryptor(){
    return MyEncryptor();
}

设置默认配置

sldp:
  enable: true # 是否启用
  # 设置默认处理器,下面这个为参数默认以json方式解析
  default-processor: top.shenluw.sldp.processor.JacksonDynamicParamsMethodProcessor
  # 设置json解析时携带数据的参数名称,就是把上面的sldpJson改为mydata 
  json-data-name: mydata
  # 设置携带实际类型的参数名称,就是把上面的sldp改为mytype
  type-name: mytype
  # 为类型设置别名
  type-alias:
      myName: top.shenluw.sldp.Dog
  # 选择json解析方式,当前可用选项 gson, jackson2, 默认 jackson2
  json-type: jackson2
  # Json解析多态时选择把class类型写入json的属性
  type-property-name: "@custom"

Jackson多态配置

可以在字段或者类上配置 @JsonTypeInfo,如果要直接使用配置文件中的type-alias,则需要添加 @JsonTypeResolver(TypeAliasResolverBuilder.class)

public class Mix extends Animal {

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    @JsonTypeResolver(TypeAliasResolverBuilder.class)
    private Animal dog;
}

Gson多态配置

使用Gson多态需要额外添加如下依赖,同时必须要配置type-alias关系才能正常使用


repositories {
    ...
    maven { url 'https://artifactory.cronapp.io/public-release/' }
}
dependencies {
    compileOnly 'com.google.code.gson:gson-extras:2.8.5'
}
The MIT License (MIT) Copyright (c) 2019 火星人 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

对spring boot mvc参数类型转换,支持转换为子类对象访问以及参数验证 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/luws/spring-dynamic-params.git
git@gitee.com:luws/spring-dynamic-params.git
luws
spring-dynamic-params
spring-dynamic-params
master

搜索帮助