42
Spring Web Services: SOAP vs. REST Sam Brannen Swi+mind GmbH

Spring Web Services: SOAP vs. REST

Embed Size (px)

DESCRIPTION

SOAP Web Services have a well established role in the enterprise, but aside from the many benefits of the WS-* standards, SOAP and XML also carry additional baggage for developers. Consequently, REST Web Services are gaining tremendous popularity within the developer community. This session will begin by comparing and contrasting the basic concepts of both SOAP and REST Web Services. Building on that foundation, Sam Brannen will show attendees how to implement SOAP-based applications using Spring-WS 2.0. He will then demonstrate how to build a similar REST-ful application using Spring MVC 3.0. The session will conclude with an in-depth look at both server-side and client-side development as well as efficient integration testing of Web Services using the Spring Framework.

Citation preview

Page 1: Spring Web Services: SOAP vs. REST

Spring Web Services:SOAP vs. REST

SamBrannenSwi+mindGmbH

Page 2: Spring Web Services: SOAP vs. REST

SpeakerProfile

•  SeniorSo+wareConsultant–Swi+mindGmbH•  Javadeveloperwith13+years'experience•  SpringFrameworkCoreDeveloper– AuthoroftheSpringTestContextFramework

•  PreviousSpringSourcedmServer™developer•  RegularspeakeratconferencesonSpring,dmServer,Java,OSGi,andtesOng

•  LeadauthorofSpringinaNutshell;chieftechnicalreviewerforSpringRecipes

Page 3: Spring Web Services: SOAP vs. REST

Agenda

•  WebServicesConcepts•  SpringEventsApplicaOon•  Spring‐WS

•  SpringREST•  Server‐side•  Client‐side•  Q&A

Page 4: Spring Web Services: SOAP vs. REST

WebServices

Page 5: Spring Web Services: SOAP vs. REST

Concepts

•  Client/ServerarchitectureovertheWeb– ServerexposesServices– ClientsendsaRequesttotheServer•  foraspecificexposedService•  withaPayloadtobeprocessedbytheService

– ServiceprocessestheRequestandreturnsaResponse

Page 6: Spring Web Services: SOAP vs. REST

MarshallingandUnmarshalling

•  Marshalling:Objectexternalformat•  Unmarshalling:externalformatObject

•  arequestorresponsecontainsapayload– o+enintheformofanXMLdocument– mayalsobebinary,JSON,etc.

•  applicaOoncodehasitsowndomainmodel– maynotmapdirectlytoformatofpayload

Page 7: Spring Web Services: SOAP vs. REST

SpringEventsApplica?on

Page 8: Spring Web Services: SOAP vs. REST

IntroducingSpringEvents

•  SimplePOJOdomainmodel:Event•  TransacOonalservicelayer•  Hibernaterepositorylayer– Withanin‐memoryHSQLdatabase

•  Spring@MVCpresentaOonlayer– RESTful@Controller

•  Spring‐WS@Endpoint

Page 9: Spring Web Services: SOAP vs. REST

SpringEventsApplicaOon

Demo

Page 10: Spring Web Services: SOAP vs. REST

SOAPWebServices

Page 11: Spring Web Services: SOAP vs. REST

WhatisSpring‐WS?

SpringWebServicesaimstofacilitatecontract‐firstSOAPservicedevelopment,allowingforthecreaAonofflexiblewebservicesusingoneofthemanywaystomanipulateXMLpayloads.(Spring‐WSWebsite)

Page 12: Spring Web Services: SOAP vs. REST

WhatisSOAP?

•  SimpleObjectAccessProtocol– ProtocolforexchanginginformaOonviaWebServices

– UsesXMLasitsmessageformat– TypicallyoverHTTP

•  Structure– Envelope– Header– Body(a.k.a.,payload)– Documentliteral

Page 13: Spring Web Services: SOAP vs. REST

ContractLast

•  DefineAPIinprogramminglanguage(e.g.,Java)

•  UsetoolstogenerateXSDsandWSDLfromcode

•  Pros–  easy,no‐brainerfordevelopers

•  Cons–  horriblyfragile–  anychangetocodebreakspreviouslypublishedcontract

–  clientsmustbeupdated

Page 14: Spring Web Services: SOAP vs. REST

ContractFirst

•  DefinepublicAPItoservicesviaXSDschemasfirst– onerequestandresponseperexposedservice– generateWSDLfromXSDsusingconvenOonsforporttypes,etc.•  potenOallyauto‐generated

– generateJavacodefromXSDs(e.g.,JAXB2)– createJavamappingcodemanually– oruseXPath,etc.toparseXMLdirectly

Page 15: Spring Web Services: SOAP vs. REST

RESTWebServices

Page 16: Spring Web Services: SOAP vs. REST

WhatisSpringREST?

•  RESTfulWebServicesbuiltontopofSpring@MVC

•  CombinesnicelywithexisOng@MVCcode

•  LowlearningcurvefordevelopersfamiliarwithSpring@MVC

•  SupportsmulOplemarshallingtechnologiessuitableforwebapplicaOons(e.g.,JSON)

•  SpringREST!=JAX‐RS

Page 17: Spring Web Services: SOAP vs. REST

WhatisREST?

•  REpresentaOonalStateTransfer•  HTTPProtocol– Standard– Ubiquitous– Scalable

•  Stateless•  Focusesonresources– NounsandVerbs

Page 18: Spring Web Services: SOAP vs. REST

Nouns,Verbs,&Errors

•  Nouns– Resourcesthatyouwanttointeractwith

•  Verbs– Whatyoucandowitharesource•  POST:createnewresource•  GET:getsingleresourceoralistofresources•  PUT:updateresource•  DELETE:deleteresource

•  Errorhandling– StandardHTTPresponsecodes

Page 19: Spring Web Services: SOAP vs. REST

RESTfulURLs

•  POST– hip://example.com/events

•  GET– hip://example.com/events– hip://example.com/events/1

•  PUT– hip://example.com/events/1

•  DELETE– hip://example.com/events/1

Page 20: Spring Web Services: SOAP vs. REST

Server‐side

Page 21: Spring Web Services: SOAP vs. REST

Spring‐WSontheServer

•  Bootstrappedinweb.xmlwithMessageDispatcherServlet

•  <sws:annotaOon‐driven/>enables@Endpointmappings(ala@Controller)

•  @PayloadRootmapshandlermethods

•  @RequestPayloadmapspayloadtomethodparameter

•  @ResponsePayloadmapsreturnvaluetoresponsepayload

Page 22: Spring Web Services: SOAP vs. REST

GetEventEndpoint(1/2)@Endpointpublic class GetEventEndpoint {

private static final String NAMESPACE_URI = "http://example.com/schemas";

private final EventService eventService;

@Autowired public GetEventEndpoint(EventService eventService) { this.eventService = eventService; }

Page 23: Spring Web Services: SOAP vs. REST

GetEventEndpoint(2/2)@PayloadRoot(localPart="GetEventRequest", namespace=NAMESPACE_URI)@ResponsePayloadpublic GetEventResponse getEvent( @RequestPayload GetEventRequest request) throws Exception {

Event event = eventService.findById(request.getId().longValue());

return toGetEventResponse(event);}

Page 24: Spring Web Services: SOAP vs. REST

SpringRESTontheServer

•  RESTWebServiceendpointsare@Controllers– @RequestMapping:mapstohandlermethods– @RequestBody:payloadofrequest– @ResponseBody:payloadofresponse– @ResponseStatus:setHTTPresponsecode– @PathVariableandUriTemplate•  FormappingandcreaOngRESTfulURIs

•  AutomaOcmarshallingofpayloads•  ContentnegoOaOon

Page 25: Spring Web Services: SOAP vs. REST

EventController(1/4)@RequestMapping("/events")@Controllerpublic class EventController {

protected final EventService eventService;

@Autowired public EventController(EventService eventService) { this.eventService = eventService; }

Page 26: Spring Web Services: SOAP vs. REST

EventController(2/4)@RequestMapping(method = GET)@ResponseBodypublic List<Event> retrieveAllEvents() { return eventService.findAllEvents();}

@RequestMapping(value = "/{id}", method = GET)@ResponseBodypublic Event retrieveEvent(@PathVariable Long id) { return eventService.findById(id);}

Page 27: Spring Web Services: SOAP vs. REST

EventController(3/4)@RequestMapping(method = POST)@ResponseStatus(HttpStatus.CREATED)public void createEvent(@RequestBody Event postedEvent, HttpServletRequest request, HttpServletResponse response) {

Event savedEvent = eventService.save(postedEvent);

String newLocation = buildNewLocation(request, savedEvent.getId());

response.setHeader("Location", newLocation);}

Page 28: Spring Web Services: SOAP vs. REST

EventController(4/4)@RequestMapping(value = "/{id}", method = DELETE)@ResponseStatus(HttpStatus.NO_CONTENT)public void deleteEvent(@PathVariable Long id) { eventService.deleteById(id);}

private String buildNewLocation(HttpServletRequest request, Long id) {

String url = request.getRequestURL() .append("/{id}").toString();

UriTemplate uriTemplate = new UriTemplate(url); return uriTemplate.expand(id).toASCIIString();}

Page 29: Spring Web Services: SOAP vs. REST

HiddenHipMethodFilter

•  WebbrowserstypicallyonlysupportGETandPOST

•  HiddenHipMethodFilter– providessupportforPUTandDELETErequestsfromwebbrowsers

–  transparentlyconvertsPOSTrequestswithhidden_methodparameter

•  Configuredinweb.xml

•  CanbeusedinconjuncOonwithSpring’sJSPformtaglibrary

Page 30: Spring Web Services: SOAP vs. REST

URITemplatesinJSPs

•  Usethe<spring>taglibrarytoconstructdynamicURItemplatesinJSPs

•  SimilartotheJSPcoretaglibrarysupportforbuildingURLswithparameters

<spring:urlvar="jsonUrl"value="/rest/events/{id}">

<spring:paramname="id"value="${event.id}"/></spring:url>

Page 31: Spring Web Services: SOAP vs. REST

Client‐side

Page 32: Spring Web Services: SOAP vs. REST

WebServiceTemplate

•  InteractwithSOAPWebServicesasaclient•  SupportscallbacksaswellasautomaOcmarshallingandunmarshallingofpayloads– sendAndReceive(…)– marshalSendAndReceive(…)

– etc.

Page 33: Spring Web Services: SOAP vs. REST

EventsSoapClientTest(1/2)@RunWith(SpringJUnit4ClassRunner.class)@ContextConfigurationpublic class EventsSoapClientTest {

@Autowired private WebServiceTemplate webServiceTemplate;

<oxm:jaxb2-marshaller id="marshaller” contextPath="com.swiftmind.samples.events.web.schema" />

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate” p:marshaller-ref="marshaller" p:unmarshaller-ref="marshaller" />

Page 34: Spring Web Services: SOAP vs. REST

EventsSoapClientTest(2/2)@Testpublic void getEventRequest() {

String url = "http://localhost:8080/spring/soap";

GetEventRequest request = new GetEventRequest(); request.setId(BigInteger.valueOf(5L));

GetEventResponse response = (GetEventResponse) webServiceTemplate.marshalSendAndReceive(url, request);

assertNotNull(response); assertEquals("Spring I/O in Madrid", response.getEventData().getDescription()); }

Page 35: Spring Web Services: SOAP vs. REST

RestTemplate

•  InteractwithanyRESTWebServices– notlimitedtoSpringRESTservices

•  SupportsURItemplatesandautomaOcmarshallingandunmarshallingofpayloads– postForLocaOon(…)– postForObject(…)– getForObject(…)– delete(…)– put(…)– etc.

Page 36: Spring Web Services: SOAP vs. REST

EventsRestClient(1/3)public class EventsRestClient {

private final RestTemplate restTemplate = new RestTemplate(); private String url;

public URI createEvent(String name) { Event event = new Event(); event.setName(name);

return restTemplate.postForLocation(url, event); }

Page 37: Spring Web Services: SOAP vs. REST

EventsRestClient(2/3)public void deleteEventByLocation(URI location) { restTemplate.delete(location);}

public void deleteEventById(Long id) { String deletionUrl = url + "/{id}";

restTemplate.delete(deletionUrl, id);}

Page 38: Spring Web Services: SOAP vs. REST

EventsRestClient(3/3)public void retrieveEvent(URI location) { Event event = restTemplate.getForObject(location, Event.class);}

public void retrieveAllEvents() { Event[] events = restTemplate.getForObject(url, Event[].class);}

Page 39: Spring Web Services: SOAP vs. REST

SpringSOAPWebServices

Demo

Page 40: Spring Web Services: SOAP vs. REST

SpringRESTWebServices

Demo

Page 41: Spring Web Services: SOAP vs. REST

FurtherResources

•  SpringFramework–  IncludingSpringREST–  hip://springframework.org

•  Spring‐WS–  hip://staOc.springsource.org/spring‐ws/sites/2.0/

•  GenerateXSDfromXML–  hip://bit.ly/fLI1Bt

Page 42: Spring Web Services: SOAP vs. REST

Q&A

SamBrannen

sam.brannen[at]swi+mind[dot]com

hip://www.swi+mind.com

hip://twiier.com/sam_brannen

“SpringinaNutshell” hip://oreilly.com/catalog/9780596801946 availablefromO’Reillyin2011