Created
March 26, 2015 10:35
-
-
Save adityasatrio/9dd795ea4d127082b93d to your computer and use it in GitHub Desktop.
Marshalling and UnMarshalling - manually using jaxb2 and spring ws
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlAccessType; | |
import javax.xml.bind.annotation.XmlAccessorType; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlType; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlRootElement(name="login", namespace = "http://service.xxx.de") | |
@XmlType(name = "login", propOrder = { "username", "password"}) | |
public class Login { | |
@XmlElement(name = "username", required = true, namespace = "http://xxx.act.de") | |
protected String username; | |
@XmlElement(name = "password", required = true, namespace = "http://xxx.act.de") | |
protected String password; | |
public Login() { | |
} | |
public Login(String username, String password) { | |
this.username = username; | |
this.password = password; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import javax.xml.bind.annotation.XmlAccessType; | |
import javax.xml.bind.annotation.XmlAccessorType; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import javax.xml.bind.annotation.XmlType; | |
import javax.xml.bind.annotation.XmlSchema; | |
import javax.xml.bind.annotation.XmlNsForm; | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlRootElement(name = "loginResponse", namespace = "http://service.xxx.de") | |
@XmlType(name = "loginResponse", propOrder = { "code", "data", "message" }) | |
public class LoginResponse { | |
@XmlElement(name = "code", required = true, namespace = "http://xxx.act.de") | |
private String code; | |
@XmlElement(name = "data", required = true, namespace = "http://xxx.act.de") | |
private DataResponse data; | |
@XmlElement(name = "message", required = true, namespace = "http://xxx.act.de") | |
private String message; | |
public LoginResponse() { | |
} | |
public LoginResponse(String code, DataResponse data, String message) { | |
this.code = code; | |
this.data = data; | |
this.message = message; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
public DataResponse getData() { | |
return data; | |
} | |
public void setData(DataResponse data) { | |
this.data = data; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
@Override | |
public String toString() { | |
return "LoginResponse[" + "message='" + message + '\'' + ", code='" + code + '\'' + ", data=" + data.toString() + ']'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
import java.util.List; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.transform.stream.StreamResult; | |
import javax.xml.transform.stream.StreamSource; | |
import org.jdom2.Element; | |
import org.jdom2.JDOMException; | |
import org.jdom2.Namespace; | |
import org.jdom2.input.SAXBuilder; | |
import org.springframework.stereotype.Service; | |
import org.springframework.ws.client.core.WebServiceTemplate; | |
import org.springframework.ws.client.core.support.WebServiceGatewaySupport; | |
import org.xml.sax.InputSource; | |
import com.xxx.ws.util.Constants; | |
import com.xxx.ws.wsdl.DataResponse; | |
import com.xxx.ws.wsdl.Login; | |
import com.xxx.ws.wsdl.LoginResponse; | |
import com.xxx.ws.wsdl.Session; | |
@Service | |
public class LoginServiceImpl extends WebServiceGatewaySupport implements LoginService { | |
private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); | |
public LoginResponse loginAuthenticate(Login login) { | |
LoginResponse loginResponse = new LoginResponse(); | |
try { | |
//marshalling login.java object | |
JAXBContext contextMarshall = JAXBContext.newInstance(Login.class); | |
Marshaller marshaller = contextMarshall.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); | |
java.io.StringWriter input = new StringWriter(); | |
marshaller.marshal(login, input); | |
//prepare output | |
StreamSource source = new StreamSource(new StringReader(input.toString())); | |
ByteArrayOutputStream bytArrayOutputStream = new ByteArrayOutputStream(); | |
StreamResult result = new StreamResult(bytArrayOutputStream); | |
//call ws soap | |
webServiceTemplate.sendSourceAndReceiveToResult(Constants.URL_WS_LOGIN, source, result); | |
String reply = new String(bytArrayOutputStream.toByteArray()); | |
//unmarshalling reply xml string to object | |
//no error but each attribute of the LoginResponse are null | |
// JAXBContext contextUnMarshall = JAXBContext.newInstance(LoginResponse.class); | |
// InputStream is = new ByteArrayInputStream(reply.getBytes()); | |
// Unmarshaller unmarshaller = contextUnMarshall.createUnmarshaller(); | |
// LoginResponse acternityResponse = (LoginResponse) unmarshaller.unmarshal(is); | |
// System.out.println(acternityResponse.toString()); | |
loginResponse = parseXMLtoLoginService(reply); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} catch (JDOMException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return loginResponse; | |
} | |
/** | |
* Parsing XML format to Object only intended to LoginResponse.java only | |
* | |
* */ | |
private LoginResponse parseXMLtoLoginService(String reply) throws JDOMException, IOException { | |
LoginResponse loginResponse = new LoginResponse(); | |
InputSource in = new InputSource(new StringReader(reply)); | |
SAXBuilder saxB = new SAXBuilder(); | |
org.jdom2.Document document = saxB.build(in); | |
Element rootNode = document.getRootElement(); // level 1 | |
Namespace namespace = Namespace.getNamespace("http://xxx.act.de"); | |
rootNode.setNamespace(namespace); | |
Element children = rootNode.getChildren().get(0); // level 2 | |
List<Element> childrenLevel3 = children.getChildren(); // level 3 | |
Element code = childrenLevel3.get(0); // getCode | |
loginResponse.setCode(code.getContent(0).getValue().toString()); // getCode value | |
Element data = childrenLevel3.get(1); // getData | |
List<Element> childrenLevel4 = data.getChildren(); | |
DataResponse dataResponse = new DataResponse(); | |
if(childrenLevel4 != null && !childrenLevel4.isEmpty()){ | |
Element addId = childrenLevel4.get(0); | |
dataResponse.setAddId(addId.getContent(0).getValue().toString()); | |
Element addType = childrenLevel4.get(1); | |
dataResponse.setAddType(addType.getContent(0).getValue().toString()); | |
Element ap3lc = childrenLevel4.get(2); | |
dataResponse.setAp3lc(ap3lc.getContent(0).getValue().toString()); | |
//in updated WS | |
// Element caId = childrenLevel4.get(3); | |
// dataResponse.setCaId(caId.getContent(0).getValue().toString()); | |
// | |
// Element sessionId = childrenLevel4.get(4); | |
// dataResponse.setSessionId(sessionId.getContent(0).getValue().toString()); | |
// | |
// Element userId = childrenLevel4.get(5); | |
// dataResponse.setUserId(userId.getContent(0).getValue().toString()); | |
//temporary because ws-hu un-updated | |
Element sessionId = childrenLevel4.get(3); | |
dataResponse.setSessionId(sessionId.getContent(0).getValue().toString()); | |
loginResponse.setData(dataResponse); | |
loginResponse.setMessage("Succesfully Logged"); | |
}else{ | |
Element message = childrenLevel3.get(2); // getMessage | |
loginResponse.setMessage(message.getContent(0).getValue().toString()); | |
} | |
return loginResponse; | |
} | |
@Override | |
public LoginResponse logout(Session session) { | |
/** | |
* untested | |
* */ | |
JAXBContext contextMarshall; | |
LoginResponse loginResponse = new LoginResponse(); | |
try { | |
contextMarshall = JAXBContext.newInstance(Session.class); | |
Marshaller marshaller = contextMarshall.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); | |
java.io.StringWriter input = new StringWriter(); | |
marshaller.marshal(session, input); | |
//prepare output | |
StreamSource source = new StreamSource(new StringReader(input.toString())); | |
ByteArrayOutputStream bytArrayOutputStream = new ByteArrayOutputStream(); | |
StreamResult result = new StreamResult(bytArrayOutputStream); | |
//call ws soap | |
webServiceTemplate.sendSourceAndReceiveToResult(Constants.ACTERNITY_WS_LOGOUT, source, result); | |
String reply = new String(bytArrayOutputStream.toByteArray()); | |
//please check the parsing if u want to use this method | |
loginResponse = parseXMLtoLoginService(reply); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} catch (JDOMException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return loginResponse; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.ws</groupId> | |
<artifactId>spring-ws-core</artifactId> | |
<version>2.2.0.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sun.xml.bind</groupId> | |
<artifactId>jaxb-xjc</artifactId> | |
<version>2.2.5</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jdom</groupId> | |
<artifactId>jdom</artifactId> | |
<version>2.0.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.jackson</groupId> | |
<artifactId>jackson-mapper-asl</artifactId> | |
<version>1.9.13</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment