zoukankan      html  css  js  c++  java
  • Java 随笔记录

    1. java对象转json

    Message msg = generateMessage();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(msg);

    2. json转java对象

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY))

    Message message = mapper.readValue(reqMsg.getBytes("GBK"), Message.class);

    3. String与xml格式的对象互相转换

    @XmlRootElement(name = "Msg")
    @lombok.Data
    public static String marshal(Object object, String encoding, String schemaLocation) throws JAXBException, UnsupportedEncodingException {
    JAXBContext context = JAXBContext.newInstance(new Class[]{object.getClass()});
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty("jaxb.encoding", encoding);
    marshaller.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
    if(schemaLocation != null && !"".equals(schemaLocation)) {
    marshaller.setProperty("jaxb.schemaLocation", schemaLocation);
    }

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    marshaller.marshal(object, outputStream);
    return outputStream.toString("UTF-8");
    }
    public static <T> T unMarshal(Class cls, String xmlStr, String encoding) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(new Class[]{cls});
    Unmarshaller unMarshaller = context.createUnmarshaller();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes(Charset.forName(encoding)));
    return unMarshaller.unmarshal(inputStream);
    }

    4. 

    
    


  • 相关阅读:
    1.8.4- 默认选中表单属性
    1.8.3- 单选框和复选按钮
    1.8.2- 文本框和密码
    springboot整合logback集成elk实现日志的汇总、分析、统计和检索功能
    elasticsearch kibana logstash(ELK)的安装集成应用
    sslopen RSA加解密
    Docker基本使用运行ngix镜像
    springCloud 之 Eureka注册中心高可用配置
    springCloud 之 Eureka服务治理
    springboot整合redis
  • 原文地址:https://www.cnblogs.com/dinglulu/p/5212880.html
Copyright © 2011-2022 走看看