我有一个 spring boot 3 项目,依赖于 com.sun.xml.bind::jaxb-impl::5.0.0, 类
@XmlRootElement(name = "Exhibitor", namespace = "http://xyz/exhibitorProfile")
public class ExhibitorType implements Serializable
{
...
}
和以下控制器代码:
RestController
public class TutorialController {
@GetMapping(value = "/test", produces = MediaType.APPLICATION_XML_VALUE)
// public JAXBElement<ExhibitonsType> getFairs(@PathVariable("lang") String language) {
public ResponseEntity<ExhibitorType> getObject() {
try {
var exhibitor = new ExhibitorType();
exhibitor.setHomepage("www.homepage");
IDType id = new IDType();
id.setValue("0815");
exhibitor.setID(id);
JAXBContext context = JAXBContext.newInstance(ExhibitorType.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(exhibitor, System.out);
return new ResponseEntity<>(exhibitor, HttpStatus.OK);
} catch (IdNotFoundException | IllegalLanguageException e) {
throw e;
} catch (Exception e) {
//return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
throw new ResponseStatusException(HttpStatusCode.valueOf(500), e.getMessage());
}
}
}
Wenn 我浏览我在浏览器中看到的 .../test url:
<ns4:Exhibitor homepage="www.homepage">
<ns1:ID>0815</ns1:ID>
</ns4:Exhibitor>
在控制台中:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:Exhibitor xmlns:ns1="http://xyz/find/common" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://xyz/query/exhibitionList" xmlns:ns4="http://xyz/query/exhibitorProfile" xmlns:ns5="http://xyz/exhibitorProgramDetails" homepage="www.homepage">
<ns1:ID>0815</ns1:ID>
</ns4:Exhibitor>
如何使命名空间在控制器响应中输出?
我用谷歌搜索和聊天,尝试了各种库版本的 jaxb-impl 但没有成功。
问候, 史蒂芬