Embed presentation








![JDOM-based message endpoint
public class EvalDomEndpoint extends AbstractJDomPayloadEndpoint
implements InitializingBean {
private Namespace namespace;
private XPath cardsXPath; private XPath suitXPath; private XPath
faceXPath
protected Element invokeInternal(Element element) throws Exception {
Card cards[] = extractCardsFromRequest(element);
PokerHand pokerHand = new PokerHand();
pokerHand.setCards(cards);
PokerHandType handType =
pokerHandEvaluator.evaluateHand(pokerHand);
return createResponse(handType);
}
private Element createResponse(PokerHandType handType) {
Element responseElement = new Element("EvaluateHandResponse",
namespace);
responseElement.addContent( new Element("handName",
namespace).setText(handType.toString()));
return responseElement;
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-9-320.jpg)
![JDOM-based message endpoint
private Card[] extractCardsFromRequest(Element element)
throws JDOMException {
Card[] cards = new Card[5];
List cardElements = cardsXPath.selectNodes(element);
for(int i=0; i < cardElements.size(); i++) {
Element cardElement = (Element) cardElements.get(i);
Suit suit = Suit.valueOf( suitXPath.valueOf(cardElement));
Face face = Face.valueOf( faceXPath.valueOf(cardElement));
cards[i] = new Card(); cards[i].setFace(face); cards[i].setSuit(suit);
}
return cards;
}
public void afterPropertiesSet() throws Exception {
namespace = Namespace.getNamespace("poker",
"http://www.springinaction.com/poker/schemas");
cardsXPath = XPath.newInstance("/poker:EvaluateHandRequest/poker.card");
cardsXPath.addNamespace(namespace);
faceXPath = XPath.newInstance("poker:face");
faceXPath.addNamespace(namespace);
suitXPath = XPath.newInstance("poker:suit");
suitXPath.addNamespace(namespace);
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-10-320.jpg)
![JDOM-based message endpoint
public class EvaluateHandJDomEndpoint extends AbstractJDomPayloadEndpoint
implements InitializingBean {
private Namespace namespace;
private XPath cardsXPath; private XPath suitXPath; private XPath faceXPath;
protected Element invokeInternal(Element element) throws Exception {
Card cards[] = extractCardsFromRequest(element);
PokerHand pokerHand = new PokerHand();
pokerHand.setCards(cards);
PokerHandType handType = pokerHandEvaluator.evaluateHand(pokerHand);
return createResponse(handType);
}
private Element createResponse(PokerHandType handType) {
Element responseElement = new Element("EvaluateHandResponse", namespace);
responseElement.addContent(new Element("handName",
namespace).setText(handType.toString()));
return responseElement;
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-11-320.jpg)
![ private Card[] extractCardsFromRequest(Element element) throws JDOMException {
Card[] cards = new Card[5];
List cardElements = cardsXPath.selectNodes(element);
for(int i=0; i < cardElements.size(); i++) {
Element cardElement = (Element) cardElements.get(i);
Suit suit = Suit.valueOf(suitXPath.valueOf(cardElement));
Face face = Face.valueOf(faceXPath.valueOf(cardElement));
cards[i] = new Card();
cards[i].setFace(face);
cards[i].setSuit(suit);
}
return cards;
}
public void afterPropertiesSet() throws Exception {
namespace = Namespace.getNamespace("poker", "http://www.springinaction.com/poker/schemas");
cardsXPath = XPath.newInstance("/poker:EvaluateHandRequest/poker.card");
cardsXPath.addNamespace(namespace);
faceXPath = XPath.newInstance("poker:face");
faceXPath.addNamespace(namespace);
suitXPath = XPath.newInstance("poker:suit");
suitXPath.addNamespace(namespace);
}
// injected
private PokerHandEvaluator pokerHandEvaluator;
public void setPokerHandEvaluator(PokerHandEvaluator pokerHandEvaluator) {
this.pokerHandEvaluator = pokerHandEvaluator;
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-12-320.jpg)



























![WebServiceTemplate Implementation
public PokerHandType evaluateHand(Card[] cards) throws IOException {
Element requestElement =
new Element("EvaluateHandRequest");
Namespace ns = Namespace.getNamespace(
"http://www.springinaction.com/poker/schemas");
requestElement.setNamespace(ns);
Document doc = new Document(requestElement);
// Process the Request message
JDOMSource requestSource = new JDOMSource(doc);
JDOMResult result = new JDOMResult();
webServiceTemplate.sendAndReceive(requestSource, result);
Document resultDocument = result.getDocument();
Element responseElement = resultDocument.getRootElement();
Element handNameElement =
responseElement.getChild("handName", ns);
return PokerHandType.valueOf(handNameElement.getText());
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-40-320.jpg)
![Marshalers on Client side
WebServiceTemplate provides marshalSendAndReceive()
method for sending and receiving XML messages that are
marshaled to and from Java objects.
public class MarshallingPokerClient implements PokerClient {
public PokerHandType evaluateHand(Card[] cards)
throws IOException {
EvaluateHandRequest request = new EvaluateHandRequest();
request.setHand(cards);
EvaluateHandResponse response = (EvaluateHandResponse)
webServiceTemplate.marshalSendAndReceive(request);
return response.getPokerHand();
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-41-320.jpg)


![Web Service Gateway Support
WebServiceGatewaySupport is a convenient support class that
automatically provides a WebServiceTemplate to client classes
that subclass it, without injecting with a WebServiceTemplate.
public class PokerServiceGateway
extends WebServiceGatewaySupport implements PokerClient {
public PokerHandType evaluateHand(Card[] cards)
throws IOException {
EvaluateHandRequest request = new EvaluateHandRequest();
request.setHand(cards);
EvaluateHandResponse response = (EvaluateHandResponse)
getWebServiceTemplate().marshalSendAndReceive(request);
return response.getPokerHand();
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-44-320.jpg)

The document describes contract-first and contract-last approaches to developing web services using Spring. It provides details on defining the service contract, creating message endpoints, mapping messages to endpoints, configuring marshaling and exceptions, and serving WSDL files. The key aspects are defining the XML schema first in contract-first, while contract-last derives the schema from the code. It also discusses using JDOM or marshalling endpoints to process messages.








![JDOM-based message endpoint
public class EvalDomEndpoint extends AbstractJDomPayloadEndpoint
implements InitializingBean {
private Namespace namespace;
private XPath cardsXPath; private XPath suitXPath; private XPath
faceXPath
protected Element invokeInternal(Element element) throws Exception {
Card cards[] = extractCardsFromRequest(element);
PokerHand pokerHand = new PokerHand();
pokerHand.setCards(cards);
PokerHandType handType =
pokerHandEvaluator.evaluateHand(pokerHand);
return createResponse(handType);
}
private Element createResponse(PokerHandType handType) {
Element responseElement = new Element("EvaluateHandResponse",
namespace);
responseElement.addContent( new Element("handName",
namespace).setText(handType.toString()));
return responseElement;
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-9-320.jpg)
![JDOM-based message endpoint
private Card[] extractCardsFromRequest(Element element)
throws JDOMException {
Card[] cards = new Card[5];
List cardElements = cardsXPath.selectNodes(element);
for(int i=0; i < cardElements.size(); i++) {
Element cardElement = (Element) cardElements.get(i);
Suit suit = Suit.valueOf( suitXPath.valueOf(cardElement));
Face face = Face.valueOf( faceXPath.valueOf(cardElement));
cards[i] = new Card(); cards[i].setFace(face); cards[i].setSuit(suit);
}
return cards;
}
public void afterPropertiesSet() throws Exception {
namespace = Namespace.getNamespace("poker",
"http://www.springinaction.com/poker/schemas");
cardsXPath = XPath.newInstance("/poker:EvaluateHandRequest/poker.card");
cardsXPath.addNamespace(namespace);
faceXPath = XPath.newInstance("poker:face");
faceXPath.addNamespace(namespace);
suitXPath = XPath.newInstance("poker:suit");
suitXPath.addNamespace(namespace);
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-10-320.jpg)
![JDOM-based message endpoint
public class EvaluateHandJDomEndpoint extends AbstractJDomPayloadEndpoint
implements InitializingBean {
private Namespace namespace;
private XPath cardsXPath; private XPath suitXPath; private XPath faceXPath;
protected Element invokeInternal(Element element) throws Exception {
Card cards[] = extractCardsFromRequest(element);
PokerHand pokerHand = new PokerHand();
pokerHand.setCards(cards);
PokerHandType handType = pokerHandEvaluator.evaluateHand(pokerHand);
return createResponse(handType);
}
private Element createResponse(PokerHandType handType) {
Element responseElement = new Element("EvaluateHandResponse", namespace);
responseElement.addContent(new Element("handName",
namespace).setText(handType.toString()));
return responseElement;
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-11-320.jpg)
![ private Card[] extractCardsFromRequest(Element element) throws JDOMException {
Card[] cards = new Card[5];
List cardElements = cardsXPath.selectNodes(element);
for(int i=0; i < cardElements.size(); i++) {
Element cardElement = (Element) cardElements.get(i);
Suit suit = Suit.valueOf(suitXPath.valueOf(cardElement));
Face face = Face.valueOf(faceXPath.valueOf(cardElement));
cards[i] = new Card();
cards[i].setFace(face);
cards[i].setSuit(suit);
}
return cards;
}
public void afterPropertiesSet() throws Exception {
namespace = Namespace.getNamespace("poker", "http://www.springinaction.com/poker/schemas");
cardsXPath = XPath.newInstance("/poker:EvaluateHandRequest/poker.card");
cardsXPath.addNamespace(namespace);
faceXPath = XPath.newInstance("poker:face");
faceXPath.addNamespace(namespace);
suitXPath = XPath.newInstance("poker:suit");
suitXPath.addNamespace(namespace);
}
// injected
private PokerHandEvaluator pokerHandEvaluator;
public void setPokerHandEvaluator(PokerHandEvaluator pokerHandEvaluator) {
this.pokerHandEvaluator = pokerHandEvaluator;
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-12-320.jpg)



























![WebServiceTemplate Implementation
public PokerHandType evaluateHand(Card[] cards) throws IOException {
Element requestElement =
new Element("EvaluateHandRequest");
Namespace ns = Namespace.getNamespace(
"http://www.springinaction.com/poker/schemas");
requestElement.setNamespace(ns);
Document doc = new Document(requestElement);
// Process the Request message
JDOMSource requestSource = new JDOMSource(doc);
JDOMResult result = new JDOMResult();
webServiceTemplate.sendAndReceive(requestSource, result);
Document resultDocument = result.getDocument();
Element responseElement = resultDocument.getRootElement();
Element handNameElement =
responseElement.getChild("handName", ns);
return PokerHandType.valueOf(handNameElement.getText());
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-40-320.jpg)
![Marshalers on Client side
WebServiceTemplate provides marshalSendAndReceive()
method for sending and receiving XML messages that are
marshaled to and from Java objects.
public class MarshallingPokerClient implements PokerClient {
public PokerHandType evaluateHand(Card[] cards)
throws IOException {
EvaluateHandRequest request = new EvaluateHandRequest();
request.setHand(cards);
EvaluateHandResponse response = (EvaluateHandResponse)
webServiceTemplate.marshalSendAndReceive(request);
return response.getPokerHand();
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-41-320.jpg)


![Web Service Gateway Support
WebServiceGatewaySupport is a convenient support class that
automatically provides a WebServiceTemplate to client classes
that subclass it, without injecting with a WebServiceTemplate.
public class PokerServiceGateway
extends WebServiceGatewaySupport implements PokerClient {
public PokerHandType evaluateHand(Card[] cards)
throws IOException {
EvaluateHandRequest request = new EvaluateHandRequest();
request.setHand(cards);
EvaluateHandResponse response = (EvaluateHandResponse)
getWebServiceTemplate().marshalSendAndReceive(request);
return response.getPokerHand();
}
}](https://image.slidesharecdn.com/15springws-130527015020-phpapp01/85/Spring-Web-Services-44-320.jpg)