Showing posts with label JAXB. Show all posts
Showing posts with label JAXB. Show all posts

9 February 2019

Java Web Services resources (updated)

31 August 2018

JAXB custom conversion using @XmlJavaTypeAdapter

To provide your own conversion implementation of a JAXB property, annotate the property in your JAXB class with javax.xml.bind.annotation.adapters.@XmlJavaTypeAdapter:

    @XmlJavaTypeAdapter(MyAdapter.class)
   
public void setFrom(LocalDate from) {
       
this.from = from;
    }

 
Then write your Adapter marshalling your LocalDate attribute to an XML value String and unmarshalling it:

    public class MyDateAdapter extends XmlAdapter<String, LocalDate> {
   
public LocalDate unmarshal(String myString) throws Exception {
     
return LocalDate.parse(myString);
    }

   
public String marshal(LocalDate myDate) throws Exception {
     
return myDate.toString();
    }

  }

23 February 2016

OU Webservices Practice 5.1 alternative

To use the Glassfish JAXB RI instead of the default Eclipse MOXy implementation in Weblogic 12.1.1 add this to the beginning of your %DOMAIN_HOME%\bin\setDomainEnv.cmd. %MW_HOME% is set to the installation location of WebLogic.

if NOT "%PRE_CLASSPATH%"=="" (set PRE_CLASSPATH=;%PRE_CLASSPATH%)
set PRE_CLASSPATH=%MW_HOME%\modules\databinding.override_1.0.0.0.jar%PRE_CLASSPATH%


On Weblogic 12.2.1 you should use WL_HOME (=%MW_HOME%\wlserver) instead of MW_HOME.

2 February 2014

Creating a JAX-WS based Dispatch client

import java.io.IOException;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.*;
import javax.xml.ws.soap.SOAPBinding;

public class DispatchClient {

    public static void main(String args[]) {

        String svcNamespace = "http://cater.com/";
        String prefix = "ns1";

        Service svc = Service.create(new QName(svcNamespace, "stockService"));
        QName portQName = new QName(svcNamespace, "stockPort");
        svc.addPort(portQName,
            SOAPBinding.SOAP11HTTP_BINDING,
            "http://localhost:7001/Stock/StockService");
        Dispatch<SOAPMessage> dispatch = svc.createDispatch(portQName,
            SOAPMessage.class,
            Service.Mode.MESSAGE);
        try {
            MessageFactory msgFactory = MessageFactory.newInstance(
                SOAPConstants.SOAP_1_1_PROTOCOL);
            SOAPMessage request = msgFactory.createMessage();
            request.getSOAPBody().
                addChildElement("getBeersFrom", prefix, svcNamespace).addChildElement(
                    "arg0").
                addTextNode("Belgium");
            SOAPMessage response = dispatch.invoke(request);
            response.writeTo(System.out);
        } catch (SOAPException | IOException ex) {
            ex.printStackTrace();
        }
    } // end main
} // end class