springmvc rest 学习笔记(1)

spring mvc

[u]http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch18s02.html[/u]

[b]18.2.1 URI Templates[/b]

///////////////////////////////////////////////////////////
RequestMethod.GET
///////////////////////////////////////////////////////////

@RequestMapping("/users/{userid}", method=RequestMethod.GET)
public String getUser(@PathVariable String userId) {
// implementation omitted...
}


@RequestMapping("/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}

@RequestMapping("/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
Owner owner = ownerService.findOwner(ownderId);
Pet pet = owner.getPet(petId);
model.addAttribute("pet", pet);
return "displayPet";
}

@Controller
@RequestMapping("/owners/{ownerId}/**")
public class RelativePathUriTemplateController {

@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}

method parameters that are decorated with the @PathVariable annotation can be of any simple type such as int, long, Date... Spring automatically converts to the appropriate type and throws a TypeMismatchException if the type is not correct.

///////////////////////////////////////////////////////////
RequestMethod.PUT
///////////////////////////////////////////////////////////

The @RequestBody method parameter annotation is used to indicate that a method parameter should be bound to the value of the HTTP request body. For example,

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}


** Mapping the request body with the @RequestBody annotation

[b]18.2.2 Returning multiple representations[/b]

A RESTful architecture may expose multiple representations of a resource. There are two strategies for a client to inform the server of the representation it is interested in receiving.

To support multiple representations of a resource Spring provides the ContentNegotiatingViewResolver to resolve a view based on the file extension or Accept header of the HTTP request.

@Controller
public class ContentController {

private List contentList = new ArrayList();

@RequestMapping(value="/content", method=RequestMethod.GET)
public ModelAndView getContent() {
ModelAndView mav = new ModelAndView();
mav.setViewName("content");
mav.addObject("sampleContentList", contentList);
return mav;
}

}

[b]18.2.3 Views[/b]

Several views were added in Spring 3 to help support creating RESTful services. They are:

*

AbstractAtomFeedView - return an Atom feed
*

AbstractRssFeedView - returns a RSS feed
*

MarshallingView - returns an XML representation using Spring's Object to XML mapping (OXM) functionality

[Note] Note

Available separately is the JacksonJsonView included as part of the Spring JavaScript project.

18.2.3.2 XML Marshalling View

[b]18.2.4 HTTP Method Conversion[/b]

[b]18.2.5 ETag support[/b]
这个先略过。

[b]18.2.6 Exception Handling[/b]

感觉 spring3 的异常处理还是比较变态的,至少比 struts2 麻烦多了。刚又看到一篇文章:http://www.oschina.net/bbs/thread/13937,难道真的这么变态么?

不过这篇文章看起来还不赖:
http://blog.csdn.net/sinlff/archive/2010/09/09/5872724.aspx

[b]其他:[/b]
有人说 springmvc 对静态资源的处理需要自己定制:
http://www.iteye.com/topic/743651

以上只是对spring 3 mvc 的一些粗略的了解。信息渠道主要是 spring3 的官方文档。具体应用方面的知识还非常欠缺。从网上找了些其他的例子:
http://www.iteye.com/topic/748006
http://ttaale.iteye.com/blog/761877
http://mxdba.iteye.com/blog/643168 (用户注册的例子)

这篇文章上有 spring3 rest 的文件上传和一个简单的demo,不错:
http://www.javabloger.com/page/9?author=1

百度贴吧上的一篇 demo:
http://tieba.baidu.com/f?kz=883344164

http://www.blogjava.net/pengo/archive/2010/07/03/325164.html

你可能感兴趣的:(REST,Spring,MVC,百度,.net)