javax.xml.bind.UnmarshalException:意外元素,即使添加了@XmlRootElement

javax.xml.bind.UnmarshalException: unexpected element even if @XmlRootElement is added

在 SoapUI 中我遇到了这个异常

<faultstring>Unmarshalling Error: unexpected element (uri:"http://ws.soap.rd.pl", local:"User"). Expected elements are <{}User></faultstring>

我真的不知道我为什么会遇到这个问题。我试过 trip gave in Stack 但仍然找不到答案。添加了@XmlRootElement 所以我真的很困惑。

要求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.soap.rd.pl/" xmlns:ws1="http://ws.soap.rd.pl">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:updateUser>
         <!--Optional:-->
         <ws1:User>
            <ID>?</ID>
            <!--Optional:-->
            <Imie>?</Imie>
            <!--Optional:-->
            <Nazwisko>?</Nazwisko>
            <!--Optional:-->
            <ws1:Adres>
               <!--Optional:-->
               <id>?</id>
               <!--Optional:-->
               <Ulica>?</Ulica>
               <!--Optional:-->
               <Miasto>?</Miasto>
            </ws1:Adres>
         </ws1:User>
      </ws:updateUser>
   </soapenv:Body>
</soapenv:Envelope>

我的 dto:

@XmlRootElement(name = "Adres", namespace = "http://ws.soap.rd.pl")
@XmlAccessorType(XmlAccessType.FIELD)
public class AddressDto {

    @XmlElement(name = "id", required = false)
    public Long id;

    @XmlElement(name = "Ulica", required = false)
    public String street;

    @XmlElement(name = "Miasto", required = false)
    public String city;

    @Override
    public String toString() {
        return "AddressDto [street=" + street + ", city=" + city + "]";
    }

}

第二个:

@XmlRootElement(name = "User", namespace = "http://ws.soap.rd.pl")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserDto {

    @XmlElement(name = "ID")
    public long id;

    @XmlElement(name = "Imie", required = false)
    public String firstName;

    @XmlElement(name = "Nazwisko", required = false)
    public String lastName;

    @XmlElement(name = "Adres", required = false)
    public AddressDto addressDto;

    @Override
    public String toString() {
        return "UserDto [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", addressDto=" + addressDto + "]";
    }

}

如果您需要我的 webService 定义:

@WebService(endpointInterface = "userService", serviceName = "userService", name = "userServive")
public interface UserService {

    @WebMethod
    public void updateUser(@WebParam(name = "User") UserDto user);
}

尝试将 targetNamespace 添加到您的 @WebParam:

@WebMethod
public void updateUser(
    @WebParam(name = "User", targetNamespace = "http://ws.soap.rd.pl")
    UserDto user);