Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

java - Send object from external API through webservice using JAXBContext and Marshaller

I've created a webservice which converts coordinates among different formats, and I've created a simple client which shows a frame where you can choose from which format you want to convert, and to what format the conversion will be done. When the user hits the button "Convert", the coordinates are sent to the conversion service and it returns the new coordinates already converted to the format specified.

I have to do this by using an external API, which has classes that define that coordinates formats properties, and I have to create instances of that classes each time I have to perform a conversion. For example:

Client Side:

//Private variable to perform the connections to the Coordinates Conversions Server
CoordinatesFormatConversionUtil conversionService;

//Marshaller to wrap an object to a XML format to be sent through the web service
private static Marshaller MARSHALLER;

{
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance( TLcdLonLatPoint.class );
        MARSHALLER = jaxbContext.createMarshaller();
        MARSHALLER.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    }
    catch ( JAXBException aE ) {
        aE.printStackTrace();
    }
}

/**
 * Function to convert from LNG/LAT to UTM through the conversion module
 */
private void lngLatToUtm() {
    String latLng, utmConverted;
    String[] latLngSplitted;

    try {
        latLng = convertFromTxtField.getText().replace(" ", "");
        latLngSplitted = latLng.split(",");
        ExternalAPILonLatPoint lonLatPoint = new ExternalAPILonLatPoint(Double.parseDouble(latLngSplitted[0]),
                Double.parseDouble(latLngSplitted[1]));
        StringWriter sw = new StringWriter();
        MARSHALLER.marshal(lonLatPoint, sw);
        String result = sw.toString();
        utmConverted = conversionService.convertFromLngLatToUtm(result);
       
        if (utmConverted != null)
            convertToTxtField.setText(utmConverted);
        else convertToTxtField.setText("CONVERSION NOT VALID");
    } catch (Exception ex) {
        convertToTxtField.setText("CONVERSION NOT VALID");
    }
}

By doing this, the following error is thrown:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: no se ha podido canalizar el tipo "com.shape.shape2D.ExternalAPILonLatPoint" como un elemento, porque le falta una anotación @XmlRootElement]

It is saying that an annotation is left, but the object to which the annotation refers, is the object from the external API.

When I generates the client package by using the wsimport command through the Terminal specifying the url of the webservice, a class of that object is created (the one that the error above refers), but with no functionality, I guess because as it is not a class that I've created, but a class from the external API.

I generate that files with the following command:

wsimport -keep -p webservice.client http://localhost:54321/coordinatesConversor?wsdl

The class auto-generated which belongs to the external API that the command above has created, is created with the following code:

package webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Clase Java para ExternalAPILonLatPoint complex type.
 * 
 * <p>El siguiente fragmento de esquema especifica el contenido que se espera que haya en esta clase.
 * 
 * <pre>
 * &lt;complexType name="ExternalAPILonLatPoint">
 *   &lt;complexContent>
 *     &lt;extension base="{http://server.webservice/}ExternalAPI2DEditablePoint">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/extension>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ExternalAPILonLatPoint")
public class ExternalAPILonLatPoint
    extends ExternalAPI2DEditablePoint
{


}

I've deleted that class, as I want to use the class that the external API provides. For sure that's the reason of the error thrown, but how can I use the original external API class instead the one created by the command?

I cannot rewrite the full ExternalAPILonLatPoint class, as it extends a chain of other internal classes of the external API so, how can I send an instance of that external API class to the server point after it is marshalled to be able in the server point to unmarshall it and perform the conversions there? There is a better way to do that? Am I wrong, or missing something important here?

Thanks for any help or suggestions.

Daniel.

question from:https://stackoverflow.com/questions/65919329/send-object-from-external-api-through-webservice-using-jaxbcontext-and-marshalle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...