java.lang.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory

java.lang.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory,java,spring,spring-boot,spring-webflux,spring-webclient,Java,Spring,Spring Boot,Spring Webflux,Spring Webclient,Spring引导版本2.4.4 Java版本15 使用SpringBoot2.4.4WebClient并尝试使用XML响应使用服务 public Mono<ServerResponse> retrieveServices() { // Headers can be passed here or while building the client Mono<DirectoryOfService> serviceMono = webClien

Spring引导版本2.4.4

Java版本15

使用SpringBoot2.4.4WebClient并尝试使用XML响应使用服务

public Mono<ServerResponse> retrieveServices() {

        // Headers can be passed here or while building the client
        Mono<DirectoryOfService> serviceMono = webClient
             .get().uri("/api/v1/test/services")
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
            .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
            .retrieve().bodyToMono(DirectoryOfService.class);

    }
格雷德尔先生

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '3.0.0'
    implementation group: 'com.sun.xml.bind', name: 'jaxb-core', version: '3.0.0'
    implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
        
}
删除com.sun.xml.bind并添加glassfish后,出现以下错误


实现“org.glassfish.jaxb:jaxb运行时:2.3.1”

错误:

org.springframework.core.codec.CodecException: Could not create JAXBContext for class [class com.test.model.DirectoryOfService]: 10 counts of IllegalAnnotationExceptions; nested exception is com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 10 counts of IllegalAnnotationExceptions
Class has two properties of the same name "section"
    this problem is related to the following location:
        at public com.test.model.Section com.test.model.DirectoryOfService.getSection()
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "name"
    this problem is related to the following location:
        at public java.lang.String com.test.model.Section.getName()
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.lang.String com.test.model.Section.name
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "test"
    this problem is related to the following location:
        at public java.util.List com.test.model.Section.getTest()
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "code"
    this problem is related to the following location:
        at public java.lang.String com.test.model.Test.getCode()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.lang.String com.test.model.Test.code
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "currency"
    this problem is related to the following location:
        at public java.lang.String com.test.model.Test.getCurrency()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.lang.String com.test.model.Test.currency
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "includes"
    this problem is related to the following location:
        at public java.lang.String com.test.model.Test.getIncludes()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.lang.String com.test.model.Test.includes
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "name"
    this problem is related to the following location:
        at public java.lang.String com.test.model.Test.getName()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.lang.String com.test.model.Test.name
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "nonDiscountable"
    this problem is related to the following location:
        at public boolean com.test.model.Test.isNonDiscountable()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private boolean com.test.model.Test.nonDiscountable
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "replicate"
    this problem is related to the following location:
        at public int com.test.model.Test.getReplicate()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private int com.test.model.Test.replicate
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
Class has two properties of the same name "validFrom"
    this problem is related to the following location:
        at public java.util.Date com.test.model.Test.getValidFrom()
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
    this problem is related to the following location:
        at private java.util.Date com.test.model.Test.validFrom
        at com.test.model.Test
        at private java.util.List com.test.model.Section.test
        at com.test.model.Section
        at private com.test.model.Section com.test.model.DirectoryOfService.section
        at com.test.model.DirectoryOfService
示例响应文件

<?xml version='1.0' encoding='UTF-8'?>
<DirectoryOfService>
    <Section Name="Directory">
        <Test>
            <Name>Mike</Name>
            <Code>USA</Code>
            <Replicate>0</Replicate>
            <ValidFrom>2016-06-30</ValidFrom>
            <Includes>Test Includes</Includes>
            <Currency>None</Currency>
            <NonDiscountable>false</NonDiscountable>
        </Test>
        <Test>
            <Name>Bingo</Name>
            <Code>USA</Code>
            <Replicate>0</Replicate>
            <ValidFrom>2016-06-30</ValidFrom>
            <Includes>Test Includes</Includes>
            <Currency>None</Currency>
            <NonDiscountable>false</NonDiscountable>
        </Test>
    </Section>
</DirectoryOfService>
模型类,

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="DirectoryOfService")
public class DirectoryOfService {

    @XmlElementRef(name = "Section")
    private Section section;

    
    public Section getSection() {
        return section;
    }

    public void setSection(Section section) {
        this.section = section;
    }

}
    @XmlRootElement(name="Section")
    public class Section {
    
        @XmlElementRef(name = "Test")
        private List<Test> test;
        @XmlAttribute(name = "Name")
        private String name;
        //public String text;
    
        public List<Test> getTest() {
            return test;
        }
    
        
        public void setTest(List<Test> test) {
            this.test = test;
        }
    
        
        public String getName() {
            return name;
        }
    
        
        public void setName(String name) {
            this.name = name;
        }
    
    }

Test class,

@XmlRootElement(name = "Test")
public class Test {

    @XmlElement(name = "Name")
    private String name;
    @XmlElement(name = "Code")
    private String code;
    @XmlElement(name = "Replicate")
    private int replicate;
    @XmlElement(name = "ValidFrom")
    private Date validFrom;
    @XmlElement(name = "Includes")
    private String includes;
    @XmlElement(name = "Currency")
    private String currency;
    @XmlElement(name = "NonDiscountable")
    private boolean nonDiscountable;

    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public String getCode() {
        return code;
    }

    
    public void setCode(String code) {
        this.code = code;
    }

    
    public int getReplicate() {
        return replicate;
    }

    
    public void setReplicate(int replicate) {
        this.replicate = replicate;
    }

    
    public Date getValidFrom() {
        return validFrom;
    }

    
    public void setValidFrom(Date validFrom) {
        this.validFrom = validFrom;
    }

    
    public String getIncludes() {
        return includes;
    }

    
    public void setIncludes(String includes) {
        this.includes = includes;
    }

    
    public String getCurrency() {
        return currency;
    }

    
    public void setCurrency(String currency) {
        this.currency = currency;
    }

    
    public boolean isNonDiscountable() {
        return nonDiscountable;
    }

    
    public void setNonDiscountable(boolean nonDiscountable) {
        this.nonDiscountable = nonDiscountable;
    }

}
服务类目录,

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="DirectoryOfService")
public class DirectoryOfService {

    @XmlElementRef(name = "Section")
    private Section section;

    
    public Section getSection() {
        return section;
    }

    public void setSection(Section section) {
        this.section = section;
    }

}
    @XmlRootElement(name="Section")
    public class Section {
    
        @XmlElementRef(name = "Test")
        private List<Test> test;
        @XmlAttribute(name = "Name")
        private String name;
        //public String text;
    
        public List<Test> getTest() {
            return test;
        }
    
        
        public void setTest(List<Test> test) {
            this.test = test;
        }
    
        
        public String getName() {
            return name;
        }
    
        
        public void setName(String name) {
            this.name = name;
        }
    
    }

Test class,

@XmlRootElement(name = "Test")
public class Test {

    @XmlElement(name = "Name")
    private String name;
    @XmlElement(name = "Code")
    private String code;
    @XmlElement(name = "Replicate")
    private int replicate;
    @XmlElement(name = "ValidFrom")
    private Date validFrom;
    @XmlElement(name = "Includes")
    private String includes;
    @XmlElement(name = "Currency")
    private String currency;
    @XmlElement(name = "NonDiscountable")
    private boolean nonDiscountable;

    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public String getCode() {
        return code;
    }

    
    public void setCode(String code) {
        this.code = code;
    }

    
    public int getReplicate() {
        return replicate;
    }

    
    public void setReplicate(int replicate) {
        this.replicate = replicate;
    }

    
    public Date getValidFrom() {
        return validFrom;
    }

    
    public void setValidFrom(Date validFrom) {
        this.validFrom = validFrom;
    }

    
    public String getIncludes() {
        return includes;
    }

    
    public void setIncludes(String includes) {
        this.includes = includes;
    }

    
    public String getCurrency() {
        return currency;
    }

    
    public void setCurrency(String currency) {
        this.currency = currency;
    }

    
    public boolean isNonDiscountable() {
        return nonDiscountable;
    }

    
    public void setNonDiscountable(boolean nonDiscountable) {
        this.nonDiscountable = nonDiscountable;
    }

}
截面计算,

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="DirectoryOfService")
public class DirectoryOfService {

    @XmlElementRef(name = "Section")
    private Section section;

    
    public Section getSection() {
        return section;
    }

    public void setSection(Section section) {
        this.section = section;
    }

}
    @XmlRootElement(name="Section")
    public class Section {
    
        @XmlElementRef(name = "Test")
        private List<Test> test;
        @XmlAttribute(name = "Name")
        private String name;
        //public String text;
    
        public List<Test> getTest() {
            return test;
        }
    
        
        public void setTest(List<Test> test) {
            this.test = test;
        }
    
        
        public String getName() {
            return name;
        }
    
        
        public void setName(String name) {
            this.name = name;
        }
    
    }

Test class,

@XmlRootElement(name = "Test")
public class Test {

    @XmlElement(name = "Name")
    private String name;
    @XmlElement(name = "Code")
    private String code;
    @XmlElement(name = "Replicate")
    private int replicate;
    @XmlElement(name = "ValidFrom")
    private Date validFrom;
    @XmlElement(name = "Includes")
    private String includes;
    @XmlElement(name = "Currency")
    private String currency;
    @XmlElement(name = "NonDiscountable")
    private boolean nonDiscountable;

    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public String getCode() {
        return code;
    }

    
    public void setCode(String code) {
        this.code = code;
    }

    
    public int getReplicate() {
        return replicate;
    }

    
    public void setReplicate(int replicate) {
        this.replicate = replicate;
    }

    
    public Date getValidFrom() {
        return validFrom;
    }

    
    public void setValidFrom(Date validFrom) {
        this.validFrom = validFrom;
    }

    
    public String getIncludes() {
        return includes;
    }

    
    public void setIncludes(String includes) {
        this.includes = includes;
    }

    
    public String getCurrency() {
        return currency;
    }

    
    public void setCurrency(String currency) {
        this.currency = currency;
    }

    
    public boolean isNonDiscountable() {
        return nonDiscountable;
    }

    
    public void setNonDiscountable(boolean nonDiscountable) {
        this.nonDiscountable = nonDiscountable;
    }

}
@XmlRootElement(name=“Section”)
公共课组{
@xmlementref(name=“Test”)
私有列表测试;
@XmlAttribute(name=“name”)
私有字符串名称;
//公共字符串文本;
公共列表getTest(){
回归试验;
}
公共无效设置测试(列表测试){
这个。测试=测试;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
考试班,
@XmlRootElement(name=“Test”)
公开课考试{
@xmlement(name=“name”)
私有字符串名称;
@xmlement(name=“Code”)
私有字符串码;
@xmlement(name=“Replicate”)
私有int复制;
@xmlement(name=“ValidFrom”)
自生效日期起的非公开日期;
@xmlement(name=“Includes”)
私有字符串包括;
@xmlement(name=“Currency”)
私人字符串货币;
@XmlElement(name=“不可贴现”)
私有布尔不可分解;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
public int getReplicate(){
返回复制;
}
公共void setReplicate(int replicate){
this.replicate=复制;
}
公共日期getValidFrom(){
返回有效日期;
}
公共无效设置生效日期(生效日期){
this.validFrom=validFrom;
}
公共字符串getIncludes(){
回报包括:;
}
公共void集合包含(字符串包含){
包括,包括;
}
公共字符串getCurrency(){
返回货币;
}
公共无效设置货币(字符串货币){
这个。货币=货币;
}
公共布尔值不可分解(){
回报不可贴现;
}
public void setNonDiscountable(布尔值nonDiscountable){
this.nonDiscountable=不可贴现;
}
}

<>代码> 虽然你测试过类似的方法,但是请考虑复习,尤其是,我认为它会有帮助。 还要确保有适当版本的JAXB运行时。在之前的问题中,Spring似乎无法在类路径中找到合适的JAXB实现。对于您在依赖项中指出的
jaxb api
版本,
2.3.1
,请删除与
com.sun.xml.bind相关的依赖项,并包括以下依赖项:

implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'
<最近的<代码> Org.Fask>代码> JAXB运行时< /COD>版本,比如代码> > Org.GassFISH。JAXB< /COD> <代码> JAXB核心< /COD>这又取决于<代码>雅加达.XML.BION/COD> >代码>雅加达.XML.BICANIAP/<代码>,基于java的Eclipse基金会JAXB实现。如果要使用
org.glassfish.jaxb:jaxb运行时:3.0.0
,请包含以下依赖项:

implementation group: 'jakarta.xml.bind', name: 'jakarta.xml.bind-api', version: '3.0.0'
与之前定义的JAXB API不同:

implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
因此,请更改您的导入并仔细测试,我从未使用Spring
WebClient
和相关的JAXB编解码器对其进行过测试

关于
部分
重复的问题,JAXB似乎试图序列化POJO的字段和方法定义的属性。为避免此问题,请使用
@xmlacessortype
注释显式定义访问类型。例如:

@XmlRootElement(name=“DirectoryOfService”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类服务目录{
@xmlementref(name=“Section”)
私人组;
公共部分getSection(){
返回段;
}
公共无效设置部分(第节){
this.section=section;
}
}
@XmlRootElement(name=“Section”)
@XmlAccessorType(XmlAccessType.FIELD)
公共课组{
@xmlementref(name=“Test”)
私有列表测试;
@XmlAttribute(name=“name”)
私有字符串名称;
//公共字符串文本;
公共列表getTest(){
回归试验;
}
公共无效设置测试(列表测试){
这个。测试=测试;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
@XmlRootElement(name=“Test”)
@XmlAccessorType(XmlAccessType.FIELD)
公开课考试{
@xmlement(name=“name”)
私有字符串名称;
@xmlement(name=“Code”)
私有字符串码;
@xmlement(name=“Replicate”)
私有int复制;
@xmlement(name=“ValidFrom”)
自生效日期起的非公开日期;
@xmlement(name=“Includes”)
私有字符串包括;
@xmlement(name=“Currency”)
私人字符串货币;
@XmlElement(name=“不可贴现”)
私有布尔不可分解;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
public int getReplicate(){