JAXB marshalling exception : Class not known to this context. with root cause

65 views Asked by At

Basically, I'm trying to consume a web service using Jaxb2 in Spring Boot, but I encounter the following error: com.example.afip.schemas.afipWsPadronA.GetPersonaV2 is not known to this context]] with root cause. The WSDL I'm using is: https://aws.afip.gov.ar/sr-padron/webservices/personaServiceA5?WSDL, and the operation I'm attempting to execute is: getPersona_v2.

Does anyone have an idea on how I can resolve this? If I try the XML that I print to the console and place it in my SoapUI project, it works perfectly. Therefore, it's evidently a problem in my implementation.

Thank you very much for any suggestions.


        GetPersonaV2 getPersonaV2 = new GetPersonaV2();
            getPersonaV2.setToken(responseWsaa.getToken());
            getPersonaV2.setSign(responseWsaa.getSign());
            getPersonaV2.setCuitRepresentada(request.getCuitRepresentada());
            getPersonaV2.setIdPersona(request.getIdPersona());

            // Reemplaza con la URL del servicio WSDL
        String wsdlUrl = "https://aws.afip.gov.ar/sr-padron/webservices/personaServiceA5?WSDL";

        // Reemplaza con el paquete generado por tu WSDL
        String requestPackage = "com.example.afip.schemas.afipWsPadronA";

        // Reemplaza con el nombre de la operaciĆ³n que deseas llamar
        String operationName = "getPersona_v2";

        //JAXBElement<GetPersonaV2> requestElement = objectFactory.createGetPersonaV2(persona);
        QName qName = new QName("http://a5.soap.ws.server.puc.sr/", "getPersona_v2", "a5");
        JAXBElement<GetPersonaV2> requestElement = new JAXBElement<>(qName, GetPersonaV2.class, null, getPersonaV2);

        // Convertir JAXBElement a XML String
        StringWriter writer = new StringWriter();
        JAXB.marshal(requestElement, writer);
        String requestXml = writer.toString();

        System.out.println("Request XML: " + requestXml);

        response =  wsPadronAdapter.get("https://aws.afip.gov.ar/sr-padron/webservices/personaServiceA5", objectFactory.createGetPersonaV2(getPersonaV2), GetPersonaV2Response.class);
        System.out.println("Respuesta: " + response); 
package com.example.afip.schemas.afipWsPadronA;

import javax.xml.bind.annotation.*;


/**
 * <p>Clase Java para getPersona_v2 complex type.
 * 
 * <p>El siguiente fragmento de esquema especifica el contenido que se espera que haya en esta clase.
 * 
 * <pre>
 * &lt;complexType name="getPersona_v2"&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;sequence&gt;
 *         &lt;element name="token" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
 *         &lt;element name="sign" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
 *         &lt;element name="cuitRepresentada" type="{http://www.w3.org/2001/XMLSchema}long"/&gt;
 *         &lt;element name="idPersona" type="{http://www.w3.org/2001/XMLSchema}long"/&gt;
 *       &lt;/sequence&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </pre>
 * 
 * 
 */

@XmlRootElement(name = "getPersona_v2", namespace = "http://a5.soap.ws.server.puc.sr/")
@XmlSeeAlso(GetPersonaV2.class)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getPersona_v2", namespace = "http://a5.soap.ws.server.puc.sr/", propOrder = {
    "token",
    "sign",
    "cuitRepresentada",
    "idPersona"
})

public class GetPersonaV2 {

    @XmlElement(required = true)
    protected String token;
    @XmlElement(required = true)
    protected String sign;
    protected long cuitRepresentada;
    protected long idPersona;

    /**
     * Obtiene el valor de la propiedad token.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getToken() {
        return token;
    }

    /**
     * Define el valor de la propiedad token.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setToken(String value) {
        this.token = value;
    }

    /**
     * Obtiene el valor de la propiedad sign.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getSign() {
        return sign;
    }

    /**
     * Define el valor de la propiedad sign.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSign(String value) {
        this.sign = value;
    }

    /**
     * Obtiene el valor de la propiedad cuitRepresentada.
     * 
     */
    public long getCuitRepresentada() {
        return cuitRepresentada;
    }

    /**
     * Define el valor de la propiedad cuitRepresentada.
     * 
     */
    public void setCuitRepresentada(long value) {
        this.cuitRepresentada = value;
    }

    /**
     * Obtiene el valor de la propiedad idPersona.
     * 
     */
    public long getIdPersona() {
        return idPersona;
    }

    /**
     * Define el valor de la propiedad idPersona.
     * 
     */
    public void setIdPersona(long value) {
        this.idPersona = value;
    }

}

package com.example.afip.adapter;

import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

import javax.xml.bind.JAXBElement;

public class WsPadronAdapter extends WebServiceGatewaySupport {

    /*public GetPersonaV2Response get(String url, Object request){
        JAXBElement res = (JAXBElement) getWebServiceTemplate().marshalSendAndReceive(url, request);
        return (GetPersonaV2Response) res.getValue();
    }*/

    public <T> T get(String url, Object request, Class<T> responseType) {
        JAXBElement<?> res = (JAXBElement<?>) getWebServiceTemplate().marshalSendAndReceive(url, request);
        return responseType.cast(res.getValue());
    }
}

Config.java

@Bean
public WsPadronAdapter soapConnectorWsPadron(Jaxb2Marshaller marshaller) {
    WsPadronAdapter client = new WsPadronAdapter();
    //String[] packagesToScan= {"com.example.afip.schemas.afip"};//TESTING
    String[] packagesToScan= {"com.example.afip.schemas.afipWsPadronA"};//PRODUCCION
    marshaller.setPackagesToScan(packagesToScan);
    //client.setDefaultUri("https://wsaahomo.afip.gov.ar/ws/services/LoginCms"); //TESTING
    client.setDefaultUri("https://aws.afip.gov.ar/sr-padron/webservices/personaServiceA5"); //PRODUCCION
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);

    return client;
}

@Bean
public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
    WebServiceTemplate template = new WebServiceTemplate();
    template.setMarshaller(marshaller);
    template.setUnmarshaller(marshaller);
    return template;
}
0

There are 0 answers