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.
5. Concepts
• Client / Server architecture over the Web
– Server exposes Services
– Client sends a Request to the Server
• for a specific exposed Service
• with a Payload to be processed by the Service
– Service processes the Request and returns a
Response
12. What is SOAP?
• Simple Object Access Protocol
– Protocol for exchanging informaOon via Web
Services
– Uses XML as its message format
– Typically over HTTP
• Structure
– Envelope
– Header
– Body (a.k.a., payload)
– Document literal
14. Contract First
• Define public API to services via XSD schemas
first
– one request and response per exposed service
– generate WSDL from XSDs using convenOons for
port types, etc.
• potenOally auto‐generated
– generate Java code from XSDs (e.g., JAXB2)
– create Java mapping code manually
– or use XPath, etc. to parse XML directly
16. What is Spring REST?
• RESTful Web Services built on top of Spring
@MVC
• Combines nicely with exisOng @MVC code
• Low learning curve for developers familiar
with Spring @MVC
• Supports mulOple marshalling technologies
suitable for web applicaOons (e.g., JSON)
• Spring REST != JAX‐RS
18. Nouns, Verbs, & Errors
• Nouns
– Resources that you want to interact with
• Verbs
– What you can do with a resource
• POST: create new resource
• GET: get single resource or a list of resources
• PUT: update resource
• DELETE: delete resource
• Error handling
– Standard HTTP response codes
19. RESTful URLs
• 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
21. Spring‐WS on the Server
• Bootstrapped in web.xml with
MessageDispatcherServlet
• <sws:annotaOon‐driven /> enables
@Endpoint mappings (a la @Controller)
• @PayloadRoot maps handler methods
• @RequestPayload maps payload to method
parameter
• @ResponsePayload maps return value to
response payload
22. GetEventEndpoint (1/2)
@Endpoint
public class GetEventEndpoint {
private static final String NAMESPACE_URI =
"http://example.com/schemas";
private final EventService eventService;
@Autowired
public GetEventEndpoint(EventService eventService) {
this.eventService = eventService;
}
24. Spring REST on the Server
• REST Web Service endpoints are
@Controllers
– @RequestMapping: maps to handler methods
– @RequestBody: payload of request
– @ResponseBody: payload of response
– @ResponseStatus: set HTTP response code
– @PathVariable and UriTemplate
• For mapping and creaOng RESTful URIs
• AutomaOc marshalling of payloads
• Content negoOaOon
35. RestTemplate
• Interact with any REST Web Services
– not limited to Spring REST services
• Supports URI templates and automaOc
marshalling and unmarshalling of payloads
– postForLocaOon(…)
– postForObject(…)
– getForObject(…)
– delete(…)
– put(…)
– etc.
36. 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);
}
41. Further Resources
• Spring Framework
– Including Spring REST
– hip://springframework.org
• Spring‐WS
– hip://staOc.springsource.org/spring‐ws/sites/2.0/
• Generate XSD from XML
– hip://bit.ly/fLI1Bt