java工具:一个支持 json、xml、yaml复制的bean 复制工具 BeanCopyUtils

项目地址

https://github.com/kylin-hunter/k-commons



前言

一个支持 json、xml、yaml复制的bean 复制工具 BeanCopyUtils


一、BeanCopyUtils

在这里插入图片描述

二、使用步骤

1 引入库

1.1 编译并发布到本地

        gradle clean build publishToMavenLocal-x test

1.2 gradle (gradle.org)


        implementation 'io.github.kylin-hunter:k-utils:1.0.2'

        // 如果需要支持yaml 才需要引用
        implementation 'org.yaml:snakeyaml:1.29' 
        // 如果需要支持xml 才需要引用
        implementation 'org.dom4j:dom4j:2.1.3' 

        // 如果支持json,才需要引入
        implementation("com.fasterxml.jackson.core:jackson-databind:2.13.4.2")
        implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4.2")
    

1.3 maven (maven.apache.org)


        <dependency>
          <groupId>io.github.kylin-hunter</groupId>
          <artifactId>k-utils</artifactId>
          <version>1.0.2</version>
        </dependency>
        <dependency>  <!-- 如果需要支持yaml 才需要引用-->
          <groupId>org.yaml</groupId>
          <artifactId>snakeyaml</artifactId>
          <version>1.29</version>
        </dependency>
        <dependency> <!-- 如果需要支持xml 才需要引用-->
          <groupId>org.dom4j</groupId>
          <artifactId>dom4j</artifactId>
          <version>2.1.3</version>
        </dependency>
        <dependency> <!-- 如果需要支持xml 才需要引用-->
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.13.4.2</version>
        </dependency>
        <dependency> <!-- 如果需要支持xml 才需要引用-->
          <groupId>com.fasterxml.jackson.datatype</groupId>
          <artifactId>jackson-datatype-jsr310</artifactId>
          <version>2.13.4.2</version>
        </dependency>

2 示例

2.1 主要Api:

   /**
 * @param source           原始bean
 * @param target           目标bean
 * @param ignoreProperties 忽略的属性
 * @return void
 * @title 拷贝属性
 * @description 拷贝属性,支持xml、json、bytes、yaml等属性的拷贝
 * @author BiJi'an
 * @date 2021/8/22 22:22
 */
public static void copyProperties(Object source, Object target, String... ignoreProperties)

2.2代码示例:

 @Data
public class Bean1 {

    private String name;

    private String nameIgnore;

    @FieldCopy(ConvertType.NUM_STR) // 支持数字和字符串之间的转换
    private int intValue1;

    @FieldCopy(ConvertType.BYTES) // 支持对象和字节数组之间的转换
    private SubBean bytes;

    @FieldCopy(ConvertType.JSON) // 支持对象和json字符串之间的转换
    private SubBean json;

    @FieldCopy(ConvertType.XML)  //支持对象和xml字符串之间的转换
    private SubBean xml;

    @FieldCopy(ConvertType.YAML) // 支持对象和yaml字符串之间的转换
    private SubBean yaml;

}

@Data
public class Bean2 {
    private String name;
    private String nameIgnore;
    private String intValue1;
    private byte[] bytes;
    private String json;
    private String xml;
    private String yaml;

}

@XmlRootElement
@Data
public class SubBean implements Serializable {
    private int id;
    private String text;
}


class BeanCopyUtilsTest {
    public static void main(String[] args) {

        SubBean subBean = new SubBean();
        subBean.setId(99);
        subBean.setText("text");

        Bean1 bean1 = new Bean1();
        bean1.setName("name");
        bean1.setNameIgnore("nameIgnore");
        bean1.setIntValue1(1);
        bean1.setBytes(subBean);
        bean1.setJson(subBean);
        bean1.setXml(subBean);
        bean1.setYaml(subBean);

        Bean2 bean2 = new Bean2();
        BeanCopyUtils.copyProperties(bean1, bean2, "nameIgnore");
        System.out.println("name=>" + bean2.getName());
        System.out.println("nameIgnore=>" + bean2.getNameIgnore());
        System.out.println("intValue1=>" + bean2.getIntValue1());
        System.out.println("bytes=>" + Arrays.toString(bean2.getBytes()));
        System.out.println("json=>" + bean2.getJson());
        System.out.println("xml=>" + bean2.getXml());
        System.out.println("yaml=>" + bean2.getYaml());

        Bean1 bean1Reverse = new Bean1();
        bean2.setNameIgnore("name2");
        BeanCopyUtils.copyProperties(bean2, bean1Reverse);

        System.out.println("name=>" + bean1Reverse.getName());
        System.out.println("nameIgnore=>" + bean1Reverse.getNameIgnore());
        System.out.println("intValue1=>" + bean1Reverse.getIntValue1());
        System.out.println("bytes=>" + bean1Reverse.getBytes());
        System.out.println("json=>" + bean1Reverse.getJson());
        System.out.println("xml=>" + bean1Reverse.getXml());
        System.out.println("yaml=>" + bean1Reverse.getYaml());

    }

}

2.3 结果输出:

 
        name=>name
        nameIgnore=>null
        intValue1=>1
        bytes=>[-84, -19, 0, 5, 115, 114, 0, 42, 105, 111, 46, 103, 105, 116, 104, 117, 98, 46, 107, 121, 108, 105, 110, 104, 117, 110, 116, 101, 114, 46, 99, 111, 109, 109, 111, 110, 115, 46, 98, 101, 97, 110, 46, 83, 117, 98, 66, 101, 97, 110, -9, -93, 3, 11, -8, 96, 40, 82, 2, 0, 2, 73, 0, 2, 105, 100, 76, 0, 4, 116, 101, 120, 116, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 120, 112, 0, 0, 0, 99, 116, 0, 4, 116, 101, 120, 116]
        json=>{"id":99,"text":"text"}
        xml=><?xml version="1.0" encoding="UTF-8" standalone="yes"?><subBean><id>99</id><text>text</text></subBean>
        yaml=>id: 99
        text: text

        name=>name
        nameIgnore=>name2
        intValue1=>1
        bytes=>SubBean(id=99, text=text)
        json=>SubBean(id=99, text=text)
        xml=>SubBean(id=99, text=text)
        yaml=>SubBean(id=99, text=text)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
消息 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置RabbitMQ连接信息 在application.yml文件中配置RabbitMQ连接信息: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 创建消息监听器 创建一个类,实现MessageListener接口,并实现onMessage方法,用于接收消息: ```java @Component public class MessageListenerImpl implements MessageListener { @Override public void onMessage(Message message) { try { String jsonStr = new String(message.getBody(), "UTF-8"); System.out.println("Received JSON message: " + jsonStr); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } ``` 4. 配置消息监听容器 创建一个SimpleMessageListenerContainer容器,配置监听队列和消息监听器: ```java @Configuration public class MessageListenerContainerConfig { @Autowired private MessageListenerImpl messageListener; @Autowired private ConnectionFactory connectionFactory; @Value("${spring.rabbitmq.queue-name}") private String queueName; @Bean public SimpleMessageListenerContainer messageListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(messageListener); return container; } } ``` 5. 发送JSON消息 使用RabbitTemplate发送JSON消息: ```java @RestController public class MessageController { @Autowired private RabbitTemplate rabbitTemplate; @Value("${spring.rabbitmq.queue-name}") private String queueName; @PostMapping("/send") public void sendMessage(@RequestBody Map<String, Object> message) { rabbitTemplate.convertAndSend(queueName, message); } } ``` 以上就是Spring Boot集成RabbitMQ接收JSON消息的基本步骤。在实际应用中,可以根据需要对消息处理进行扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羲圣

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值