gpt4 book ai didi

java - 将 java 对象转换为 XML REST 响应

转载 作者:行者123 更新时间:2023-12-01 17:50:34 25 4
gpt4 key购买 nike

我想在 API REST 调用后返回 XML 响应消息。我尝试了这个简单的测试:

@RestController
public class HelloWorldRestController {

@Autowired
ApiService apiService;
@RequestMapping(value = "/api", method = RequestMethod.GET)
public ResponseEntity<VisaResponse> listAllUsers() {
VisaResponse obj = apiService.visaresponse();
return new ResponseEntity<VisaResponse>(obj, HttpStatus.OK);
}
.......
}

我尝试将简单对象转换为 XML

public class VisaResponse {

public VisaResponse() {
jaxbObjectToXML(new VisaResponseHeader());
}

private static String jaxbObjectToXML(VisaResponseHeader customer) {
String xmlString = "";
try {
JAXBContext context = JAXBContext.newInstance(VisaResponseHeader.class);
Marshaller m = context.createMarshaller();

m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML

StringWriter sw = new StringWriter();
m.marshal(customer, sw);
xmlString = sw.toString();

} catch (JAXBException e) {
e.printStackTrace();
}
return xmlString;
}
}

public class VisaResponseHeader {

private int id;

public VisaResponseHeader() {
id = 3;
}
}

但是当我发出休息请求时,没有任何反应 - 没有任何响应。你知道我错在哪里吗?应返回简单内容。

最佳答案

  1. 在@RequestMapping 中添加 Produces = "application/xml"@RequestMapping(value="/api", method=RequestMethod.GET, Produce=application/xml")
  2. 您需要向您的 Bean 类 (VisaResponseHeader) 添加一些 JAXB 注释,以允许将其编码到 XML 中。

@XmlRootElement:此注释在顶级类中使用,用于指示 XML 文档中的根元素。注释中的 name 属性是可选的。如果未指定,则类名将用作文档中的根 XML 元素。

@XmlAttribute:该注解用于指示根元素的属性。

@XmlElement:此注释用于将成为根元素的子元素的类的属性。

HelloWorldRestController.java

@RestController
public class HelloWorldRestController {

@Autowired
ApiService apiService;

@RequestMapping(value = "/api", method = RequestMethod.GET)
public ResponseEntity<String> listAllUsers() {
// get data from database
VisaResponse visaResponse = apiService.visaresponse();

// convert bean to XML
String xmlResponse = jaxbObjectToXML(visaResponse);

return new ResponseEntity<>(xmlResponse, HttpStatus.OK);
}

private static String jaxbObjectToXML(VisaResponse customer) {
String xmlString = "";
try {
JAXBContext context = JAXBContext.newInstance(VisaResponse.class);
Marshaller m = context.createMarshaller();

m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
m.marshal(customer, sw);
xmlString = sw.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlString;
}
}

VisaResponse.java

    @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VisaResponse {

@XmlElement
private int id;

// for testing purpose. Remove once database integration is done and data is received via service and repository.
public VisaResponse() {
id = 3;
}
}

不需要 VisaResponseHeader.java 类。

预期输出(使用Postman测试) enter image description here

关于java - 将 java 对象转换为 XML REST 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50601912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com