Создаем web-сервисы restful-стиля При Помощи Функции spring 3 mvc...
TRANSCRIPT
-
Copyright IBM Corporation 2011 Web- RESTful- Spring 3 MVC HttpMessageConverter
1 13
Web- RESTful- Spring 3 MVC HttpMessageConverter -IBM
04.07.2011
Spring, Java Platform EnterpriseEdition (Java EE), (Representational State Transfer REST) --(Model-View-Controller MVC). Web- RESTful , . , HttpMessageConverter. , RestTemplate HttpMessageConverter . , , Spring Web- RESTful-, , ATOM Feed, XML JavaScript Object Notation (JSON).
" Web- RESTful Spring 3" (. ) Web- RESTful Sping. , ContentNegotiatingViewResolver , Web- RESTful. HttpMessageConverter, RestTemplate HttpMessageConverter .
REST Spring MVC
Spring ( ), Web- RESTful.
@Controller @Controller, , MVC HTTP-.
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
2 13
@RequestMapping @RequestMapping, , HTTP-, URI HTTP-. REST Spring. method HTTP-.:
@RequestMapping(method=RequestMethod.GET, value="/emps",headers="Accept=application/xml, application/json")
@PathVariable @PathVariable URI .:
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}")public ModelAndView getEmployee(@PathVariable String id) { }
@RequestParam, URL- . @RequestHeader, HTTP- .
@RequestBody, HTTP- .
@ResponseBody, HTTP-.
HttpEntity , .
ResponseEntity, HTTP- .
:
public @ResponseBody Employee getEmployeeBy(@RequestParam("name")String name, @RequestHeader("Accept") String accept, @RequestBody String body) {}public ResponseEntity method(HttpEntity entity) {}
Spring (. ) , .
MIME- RESTful Web-. , URI HTTP- . URI URI .
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
3 13
" Web- RESTful Spring 3" (. ) ContentNegotiatingViewResolver, URI ( ). , ContentNegotiatingViewResolver .
HttpMessageConverter @ResponseBody. View.
HttpMessageConverterHTTP- , .. . Spring String ( Java-). Spring / ? HttpMessageConverter. Spring , . 1 .
1. HttpMessageConverter
... ...
StringHttpMessageConverter / . - text/* Content-Type, text/plain.
FormHttpMessageConverter / . - pplication/x-www-form-urlencoded MultiValueMap.
MarshallingHttpMessageConverter / XML- / Spring. -application/xml.
MappingJacksonHttpMessageConverter / JSON- Jackson'sObjectMapper. - application/json.
AtomFeedHttpMessageConverter / ATOM ROME Feed API. - application/atom+xml.
RssChannelHttpMessageConverter / RSS ROME Feed API. - application/rss+xml.
Web- RESTful , Web- RESTful, . , , " Web- RESTful Spring 3" (. ). .
-, HttpMessageConverter. , HttpMessageConverter
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
4 13
-. - JSON,ATOM XML.
JSON
. JSON - , . 1 , JSON.
1. HttpMessageConverter rest-servlet.xml
.MappingJacksonHttpMessageConverter JSON . Jackson ObjectMapper JSON JavaBean, Jackson JAR- classpath.
org.codehaus.jackson.jar org.codehaus.jackson.mapper.jar
JSON. 2 .
2. JSON-, EmployeeController@RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/json")public @ResponseBody Employee getEmp(@PathVariable String id) {Employee e = employeeDS.get(Long.parseLong(id));return e;}
@RequestMapping(method=RequestMethod.GET, value="/emps", headers="Accept=application/json")public @ResponseBody EmployeeListinggetAllEmp() {List employees = employeeDS.getAll();EmployeeListinglist = new EmployeeList(employees);return list;}
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
5 13
@ResponseBody , (Employee EmployeeList) , MappingJacksonHttpMessageConverter JSON.
HttpMessageConverter @ResponseBody , Spring View, ContentNegotiatingViewResolver.
CURL REST- Firefox . HTTP-: Accept=application/json. 3 JSON.
3. JSON- getEmp() getAllEmp()Response for /rest/service/emp/1{"id":1,"name":"Huang Yi Ming","email":"[email protected]"}
Response for /rest/service/emps{"count":2,"employees":[{"id":1,"name":"Huang Yi Ming","email":"[email protected]"},{"id":2,"name":"Wu Dong Fei","email":"[email protected]"}]}
XML Spring MarshallingHttpMessageConverter XML (OXM). JAXB 2 / . 4 .
4. MarshallingHttpMessageConverter
dw.spring3.rest.bean.Employee dw.spring3.rest.bean.EmployeeList
, JAXB 2 java.util.List XML. - . " Web- RESTful Spring 3" (.) , JAXB-.
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
6 13
, ? 2. , . - Accept:
headers=Accept=application/json, application/xml
(JSON XML). 5 application/xml.
5. XML- getEmp() getAllEmp()Response for /rest/service/emp/1
[email protected] 1 Huang Yi Ming Response for /rest/service/emps
2 [email protected] 1 Huang Yi Ming [email protected] 2Wu Dong Fei
ATOM ATOM - Web- RESTful. Atom Atom, ( ) , . atom:feed. ATOM Publish Protocol (APP), . ( ATOM APP- . . .)
AtomFeedHttpMessageConverter ATOM, ROME ATOM API. JAR-sun.syndication.jar classpath. 6 .
6. AtomFeedHttpMessageConverter
7 , ATOM .
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
7 13
7. getEmpFeed() EmployeeController & AtomUtil@RequestMapping(method=RequestMethod.GET, value="/emps", headers="Accept=application/atom+xml")public @ResponseBody Feed getEmpFeed() { List employees = employeeDS.getAll(); return AtomUtil.employeeFeed(employees, jaxb2Mashaller);}
public static Feed employeeFeed( List employees, Jaxb2Marshaller marshaller) {Feed feed = new Feed();feed.setFeedType("atom_1.0");feed.setTitle("Employee Atom Feed");
List entries = new ArrayList();for(Employee e : employees) { StreamResult result = new StreamResult( new ByteArrayOutputStream()); marshaller.marshal(e, result); String xml = result.getOutputStream().toString();
Entry entry = new Entry(); entry.setId(Long.valueOf(e.getId()).toString()); entry.setTitle(e.getName()); Content content = new Content(); content.setType(Content.XML); content.setValue(xml);
List contents = new ArrayList(); contents.add(content); entry.setContents(contents); entries.add(entry);}feed.setEntries(entries);return feed;}
, :
GetEmpFeed() URI, getAllEmp(), Accept.
employeeFeed() Employee XML, .
8 application/atom+xml URI /rest/service/emps.
8. /rest/service/emps application/atom+xml
Employee Atom Feed
Huang Yi Ming 1 [email protected] 1
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
8 13
Huang Yi Ming
Wu Dong Fei 2 [email protected] 2 Wu Dong Fei
POST, PUT DELETE HTTP- GET. 9 POST, PUT DELETE.
9. POST, PUT DELETE EmployeeController@RequestMapping(method=RequestMethod.POST, value="/emp")public @ResponseBody Employee addEmp(@RequestBody Employee e) {employeeDS.add(e);return e;}
@RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")public @ResponseBody Employee updateEmp( @RequestBody Employee e, @PathVariable String id) {employeeDS.update(e);return e;}
@RequestMapping(method=RequestMethod.DELETE, value="/emp/{id}")public @ResponseBody void removeEmp(@PathVariable String id) {employeeDS.remove(Long.parseLong(id));}
@RequestBody addEmp() updateEmp(). HTTP- , HttpMessageConverter. RestTemplate REST-.
RestTemplate REST- " Web- RESTful Spring 3" (. ), CURL REST- REST-. Jakarta Commons HttpClient( ). Spring REST- RestTemplate. Spring, JdbcTemplate JmsTemplate.
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
9 13
RestTemplate HttpMessageConverter. .
RestTemplate
10 RestTemplate. .
10. RestTemplate
, . RestTemplate , :
exchange: HTTP- . getForObject: HTTP- GET . postForObject: HTTP- POST . put: HTTP- PUT . delete: HTTP- DELETE URI.
RestTemplate. RestTemplate API (. ), API.
11 , , . MarshallingHttpMessageConverter, . -.
11. XML-HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_XML);HttpEntity entity = new HttpEntity(headers);ResponseEntity response = restTemplate.exchange("http://localhost:8080/rest/service/emps",HttpMethod.GET, entity, EmployeeList.class);EmployeeListingemployees = response.getBody();// employees
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
10 13
12 POST. AddEmp() - application/xml application/json.
12. POST
Employee newEmp = new Employee(99, "guest", "[email protected]");HttpEntity entity = new HttpEntity(newEmp);ResponseEntity response = restTemplate.postForEntity("http://localhost:8080/rest/service/emp", entity, Employee.class);Employee e = response.getBody();// employee
13 , PUT. - ({id}) URI .
13. PUT
Employee newEmp = new Employee(99, "guest99", "[email protected]");HttpEntity entity = new HttpEntity(newEmp);restTemplate.put( "http://localhost:8080/rest/service/emp/{id}", entity, "99");
14 , DELETE.
14. DELETE
restTemplate.delete( "http://localhost:8080/rest/service/emp/{id}", "99");
HttpMessageConverter, Spring 3. , . , HttpMessageConverter , ContentNegotiatingViewResolver " Web- RESTful Spring 3".
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
11 13
src_code.zip 11
-
developerWorks ibm.com/developerWorks/ru/
Web- RESTful- Spring 3 MVC HttpMessageConverter
12 13
Build RESTful web services with the Spring 3 MVC HttpMessageConverterfeature (EN).
Web- RESTful Spring 3(developerWorks, 2010 , EN) Web-RESTful Spring.
REST .
Spring MVC Showcase, , . , .
Spring 3. RestTemplate API. JAXB Reference Implementation Project. ROME - Java- Atom/RSS, Java
. ATOM. DeveloperWorks Atom Publishing Protocol
( 2006 .), , (EN).
Jackson - JSON- .
Spring. Jackson. IBM
IBM SOA Sandbox, DB2, Lotus, Rational,Tivoli WebSphere.
developerWorks, Web-. Web- Web-
. Web 2.0
. Web-, developerWorks.
-
ibm.com/developerWorks/ru/ developerWorks
Web- RESTful- Spring 3 MVC HttpMessageConverter
13 13
(Yi Ming Huang) - - China Development Lab, Mashup- Lotus. Web- . REST, OSGi Spring.
Copyright IBM Corporation 2011(www.ibm.com/legal/copytrade.shtml) (www.ibm.com/developerworks/ru/ibm/trademarks/)
REST Spring MVC
HttpMessageConverter Web- RESTfulJSONXML ATOM POST, PUT DELETE
RestTemplate REST- RestTemplate