在的Java / Spring项目组织版本的REST API code的最佳实践(例子吗?)? [英] Best practices (examples?) on organizing versioned REST API code in Java/Spring project?

查看:217
本文介绍了在的Java / Spring项目组织版本的REST API code的最佳实践(例子吗?)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我珍玩,如果有人可以点我一个很好的例子/组织REST API code与版本控制的最佳实践(URI为主,意为/ V1 / ZZZ和/ V2 / XXX,甚至更好一些依靠Accept头) - 用Java / Spring项目?我怕我使之在我的项目太复杂了,现在,所以这会是很好的向别人学习。

I am curios if anybody can point me to a good example/best practice of organizing REST API code with versioning (uri-based, means "/v1/zzz" and "/v2/xxx" or even better something relying on Accept header) -- in Java/Spring project? I am afraid I am making it too complicated in my project right now, so it'd be nice to learn from others.

澄清:不知道我是否应该这样做,通过过滤器,然后使用一些设计模式去改变,但是这样会让我的过滤器pretty复杂..或者可能是我没有想到的一些诀窍与春天和DI,这样我就可以让我的code清洁。最简单的方法是在每一个里面会有不同的版本的一些方法策略模式,但它似乎并没有太干净的既不是: - (

Clarification: not sure if I should do it through filters, and then use some design pattern to change the behavior, but this will make my filter pretty complicated.. or may be I am not thinking about some trick with spring and DI, so I can make my code cleaner. the simplest approach is some strategy pattern in every method which will have different versioning, but it doesn't seem to be too clean neither :-(

推荐答案

我强烈建议阅读Apigee的书和博客的 http://offers.apigee.com/api-design-ebook-bw/ 我发现,它给了我真正实用的建议设计的URL和做错误处理。

I highly recommend reading the book and blogs on Apigee http://offers.apigee.com/api-design-ebook-bw/ I found that it gave me really practical advice for designing the urls and doing error handling.

http://www.stormpath.com/blog/spring-mvc-rest-exception-handling-best-practices-part-2对如何配置Spring MVC的为RESTful应用程序做一般性的错误处理一个真正伟大的文章。

http://www.stormpath.com/blog/spring-mvc-rest-exception-handling-best-practices-part-2 has a really great article on how to configure spring mvc to do generic error handling for restful applications.

至于在接受headrs即pretty容易做到,因为弹簧让你缩小基于所述过滤器的处理程序方法的映射,如在标题=在下面的请求映射滤波。

As for filtering on accept headrs that is pretty easy to do since spring lets you narrow a handler method mapping based on the filter, as in the headers= in the request mapping below.

@RequestMapping(value="/narrow/headers/{name}/{email}/{customerNumber}",
        method={RequestMethod.POST,RequestMethod.GET},
        headers="Referer=http://localhost:8080/SpringMVC/request-mappings.html")
public ResponseEntity<String> narrowOnHeaders(
        @PathVariable("name")String name, 
        @PathVariable("email") String email, 
        @PathVariable("customerNumber") Integer customerNumber,
        @RequestHeader("Referer") String referer
    )

这篇关于在的Java / Spring项目组织版本的REST API code的最佳实践(例子吗?)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆