04.07.2011
@Controller, ,
MVC HTTP-.
Copyright IBM Corporation 2011
Web- RESTful-
Spring 3 MVC HttpMessageConverter
1 13
developerWorks
ibm.com/developerWorks/ru/
@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<T> ,
.
ResponseEntity<T>, HTTP-
.
:
public @ResponseBody Employee getEmployeeBy(@RequestParam("name")
String name, @RequestHeader("Accept") String accept, @RequestBody String body) {}
public ResponseEntity<String> method(HttpEntity<String> entity) {}
Spring (. )
, .
MIME-
RESTful Web-. ,
URI HTTP-
. URI URI
.
Web- RESTful-
Spring 3 MVC HttpMessageConverter
2 13
ibm.com/developerWorks/ru/
developerWorks
HttpMessageConverter
HTTP- , ..
. Spring
String ( Java-).
Spring / ?
HttpMessageConverter. Spring ,
. 1 .
1. HttpMessageConverter
...
...
StringHttpMessageConverter
/ .
- text/*
Content-Type, text/plain.
FormHttpMessageConverter
/ .
- pplication/x-www-form-urlencoded
MultiValueMap<String,String>.
MarshallingHttpMessageConverter
/ XML- /
Spring. -
application/xml.
MappingJacksonHttpMessageConverter
/ JSON- Jackson's
ObjectMapper. - application/
json.
AtomFeedHttpMessageConverter
RssChannelHttpMessageConverter
Web- RESTful
, Web- RESTful,
. ,
, " Web- RESTful
Spring 3" (. ). .
-, HttpMessageConverter.
, HttpMessageConverter
Web- RESTful-
Spring 3 MVC HttpMessageConverter
3 13
developerWorks
ibm.com/developerWorks/ru/
-. - JSON,
ATOM XML.
JSON
. JSON - ,
. 1 ,
JSON.
1. HttpMessageConverter rest-servlet.xml
<bean class="org.springframework.web.servlet.mvc.annotation
.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="marshallingConverter" />
<ref bean="atomConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json
.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
.
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<Employee> employees = employeeDS.getAll();
EmployeeListinglist = new EmployeeList(employees);
return list;
}
Web- RESTful-
Spring 3 MVC HttpMessageConverter
4 13
ibm.com/developerWorks/ru/
developerWorks
@ResponseBody ,
(Employee EmployeeList) ,
MappingJacksonHttpMessageConverter JSON.
HttpMessageConverter @ResponseBody
, Spring View,
ContentNegotiatingViewResolver.
CURL REST- Firefox .
HTTP-: Accept=application/json. 3
JSON.
XML
Spring MarshallingHttpMessageConverter
XML (OXM). JAXB 2
/ . 4 .
4. MarshallingHttpMessageConverter
<bean id="marshallingConverter"
class="org.springframework.http.converter.xml
.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jaxbMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>dw.spring3.rest.bean.Employee</value>
<value>dw.spring3.rest.bean.EmployeeList</value>
</list>
</property>
</bean>
, JAXB 2 java.util.List<T>
XML. -
. " Web- RESTful Spring 3" (.
) ,
JAXB-.
Web- RESTful-
Spring 3 MVC HttpMessageConverter
5 13
developerWorks
ibm.com/developerWorks/ru/
, ?
2. , .
- Accept:
headers=Accept=application/json, application/xml
(JSON XML).
5 application/xml.
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
<bean id="atomConverter"
class="org.springframework.http.converter.feed
.AtomFeedHttpMessageConverter">
<property name="supportedMediaTypes" value="application/atom+xml" />
</bean>
7 , ATOM .
Web- RESTful-
Spring 3 MVC HttpMessageConverter
6 13
ibm.com/developerWorks/ru/
developerWorks
, :
GetEmpFeed() URI, getAllEmp(),
Accept.
employeeFeed() Employee XML,
<content> .
8 application/atom+xml URI /
rest/service/emps.
8. /rest/service/emps application/atom
+xml
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Employee Atom Feed</title>
<entry>
<title>Huang Yi Ming</title>
<id>1</id>
<content type="xml">
<employee>
<email>huangyim@cn.ibm.com</email>
<id>1</id>
Web- RESTful-
Spring 3 MVC HttpMessageConverter
7 13
developerWorks
ibm.com/developerWorks/ru/
<name>Huang Yi Ming</name>
</employee>
</content>
</entry>
<entry>
<title>Wu Dong Fei</title>
<id>2</id>
<content type="xml">
<employee>
<email>wudongf@cn.ibm.com</email>
<id>2</id>
<name>Wu Dong Fei</name>
</employee>
</content>
</entry>
</feed>
RestTemplate REST
" Web- RESTful Spring 3" (. )
, CURL REST- REST-.
Jakarta Commons HttpClient
( ). Spring REST RestTemplate.
Spring, JdbcTemplate JmsTemplate.
Web- RESTful-
Spring 3 MVC HttpMessageConverter
8 13
ibm.com/developerWorks/ru/
developerWorks
HttpMessageConverter.
.
RestTemplate
RestTemplate
10 RestTemplate.
.
10. RestTemplate
<bean id="restTemplate"
class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
<ref bean="atomConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
,
. 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<String> entity = new HttpEntity<String>(headers);
ResponseEntity<EmployeeList> response = restTemplate.exchange(
"http://localhost:8080/rest/service/emps",
HttpMethod.GET, entity, EmployeeList.class);
EmployeeListingemployees = response.getBody();
// employees
Web- RESTful-
Spring 3 MVC HttpMessageConverter
9 13
developerWorks
ibm.com/developerWorks/ru/
12 POST.
AddEmp() - application/
xml application/json.
12. POST
Employee newEmp = new Employee(99, "guest", "guest@ibm.com");
HttpEntity<Employee> entity = new HttpEntity<Employee>(newEmp);
ResponseEntity<Employee> 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", "guest99@ibm.com");
HttpEntity<Employee> entity = new HttpEntity<Employee>(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".
Web- RESTful-
Spring 3 MVC HttpMessageConverter
10 13
ibm.com/developerWorks/ru/
developerWorks
src_code.zip
11
Web- RESTful-
Spring 3 MVC HttpMessageConverter
11 13
developerWorks
ibm.com/developerWorks/ru/
developerWorks, Web-.
Web- Web.
Web 2.0
.
Web-, developerWorks.
Web- RESTful-
Spring 3 MVC HttpMessageConverter
12 13
ibm.com/developerWorks/ru/
developerWorks
(Yi Ming Huang) - - China Development Lab,
Mashup- Lotus. Web-
. REST, OSGi Spring.
Web- RESTful-
Spring 3 MVC HttpMessageConverter
13 13