spring Sping Boot 3控制器返回没有命名空间的JAXB类

6qftjkof  于 2023-05-05  发布在  Spring
关注(0)|答案(1)|浏览(174)

我有一个 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 I browse my.../test url I see in Browser:

<ns4:Exhibitor homepage="www.homepage">
<ns1:ID>0815</ns1:ID>
</ns4:Exhibitor>

在Console中:

<?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>

如何使命名空间能够在控制器响应中输出?
我在google上搜索和聊天,尝试了各种库版本的jaxb-impl,但都没有成功。

6yt4nkrj

6yt4nkrj1#

问题是Firefox中的XML表示。网络控制台中的完整XML包含名称空间。在Chrome中也显示正确

相关问题