TestJsonController
package com.xxx.api.controller.business;
import com.xxx.business.domain.Body;
import com.xxx.business.domain.Business;
import com.xxx.business.domain.Fyxm;
import com.xxx.business.domain.Group;
import com.xxx.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
@Api(tags = "api接口")
@RestController
@RequestMapping("/api")
@Slf4j
public class TestJsonController {
@ApiOperation(value = "测试")
@PostMapping("/test")
public AjaxResult test() throws JAXBException {
Business business = new Business();
Body body = new Body();
body.setYylxdm("1");
Fyxm fyxm = new Fyxm();
fyxm.setCount("1");
Group group = new Group();
group.setXh("1");
group.setDj("dj");
fyxm.setGroup(group);
body.setFyxm(fyxm);
business.setBody(body);
business.setComment("ceshi");
business.setId(30010);
String result = beanToXml(business, Business.class);
System.out.println(result);
return AjaxResult.success();
}
private String beanToXml(Object obj, Class<?> load) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
}
}
实体类
Business
package com.xxx.business.domain;
import lombok.Data;
import javax.xml.bind.annotation.*;
@Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Business {
@XmlAttribute
private int id;
@XmlAttribute
private String comment;
private Body body;
}
Body
package com.xxx.business.domain;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
@XmlAttribute
private String yylxdm;
private String kplx;
private String qdbz;
private String zsfs;
private Fyxm fyxm;
}
Fyxm
package com.xxx.business.domain;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Fyxm {
@XmlAttribute
private String count;
private Group group;
}
Group
package com.xxx.business.domain;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Group {
@XmlAttribute
private String xh;
private String fphxz;
private String spmc;
private String dj;
}