-
为所有“事物”定义ID
-
将所有事物链接在一起
-
使用标准方法
-
资源多重表述
-
无状态通信
-
URI,比如:http://example.com/resources/。
-
Web Service接受与返回的互联网媒体类型,比如:JSON,XML等。
-
Web Service在该资源上所支持的一系列请求方法(比如:POST,GET,PUT或DELETE)。
1
2
3
4
5
6
7
8
9
10
11
|
@Controller
@RequestMapping
(value =
"/contact"
)
public
class
ContactController {
final
Logger logger = LoggerFactory.getLogger(ContactController.
class
);
@Autowired
private
ContactService contactService;
@RequestMapping
(value =
"/listdata"
, method = RequestMethod.GET)
@ResponseBody
public
Contacts listData() {
return
new
Contacts(contactService.findAll());
}
|
1
2
3
4
5
|
@RequestMapping
(value =
"/{id}"
, method = RequestMethod.GET)
@ResponseBody
public
Contact findContactById(
@PathVariable
Long id) {
return
contactService.findById(id);
}
|
1
2
3
4
5
6
7
8
|
@RequestMapping
(value =
"/"
, method = RequestMethod.POST)
@ResponseBody
public
Contact create(
@RequestBody
Contact contact) {
logger.info(
"Creating contact: "
+ contact);
contactService.save(contact);
logger.info(
"Contact created successfully with info: "
+ contact);
return
contact;
}
|
1
2
3
4
5
|
@RequestMapping
(value =
"/listdata"
, method = RequestMethod.GET)
@ResponseBody
public
Contacts listData() {
return
new
Contacts(contactService.findAll());
}
|
1
2
3
4
5
6
7
8
9
10
|
<
mvc:annotation-driven
>
<
mvc:message-converters
>
<
bean
class
=
"org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
/>
<
bean
class
=
"org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
>
<
property
name
=
"marshaller"
ref
=
"castorMarshaller"
/>
<
property
name
=
"unmarshaller"
ref
=
"castorMarshaller"
/>
</
bean
>
</
mvc:message-converters
>
</
mvc:annotation-driven
>
|
-
ByteArrayHttpMessageConverter – converts byte arrays
-
StringHttpMessageConverter – converts Strings
-
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
-
SourceHttpMessageConverter – converts javax.xml.transform.Source
-
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
-
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
-
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
-
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
-
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
-
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)