[英]What is a good technology stack for a java web based application with REST support?
Evening all :) 晚上全部:)
I'm looking to create a Java web application. 我正在寻找创建Java Web应用程序。 I envisage that it will use Spring web MVC and JSPs, however I would like to expose certain functionality as REST calls so I can create an android client. 我设想它将使用Spring Web MVC和JSP,但是我想将某些功能公开为REST调用,因此我可以创建一个Android客户端。
Does spring offer anything to help me in this area? 春天有什么能帮助我在这个地区吗? How can I keep the REST code and the web front end code separate yet not have to maintain essentially 2 versions of my application (one for the web, one for REST clients). 如何将REST代码和Web前端代码分开,而不必维护我的应用程序的基本上2个版本(一个用于Web,一个用于REST客户端)。
Not looking for spoon feeding, just some pointers of where I should start reading. 不是寻找勺子喂食,只是我应该开始阅读的一些指示。
As others have mentioned, Spring has pretty good in-built REST support now. 正如其他人所提到的,Spring现在拥有相当不错的内置REST支持。 When combined with annotations, this allows really simple set-up of a RESTful API. 与注释结合使用时,可以非常简单地设置RESTful API。 Spring can be configured with different view resolvers , which can automatically respond with a different view of the data depending on the Accept
header for example. 可以使用不同的视图解析器配置Spring,例如,可以根据Accept
标头自动响应不同的数据视图。 So you could return either JSON or JSP automatically from the same data, see the ContentNegotiatingViewResolver . 因此,您可以从相同的数据中自动返回JSON或JSP,请参阅ContentNegotiatingViewResolver 。 Your Controller and Model can then be common and implemented once, leaving the work in the View layer. 您的控制器和模型可以是常见的并实现一次,将工作留在View层中。
I've used this approach before to return JSON when the request was via AJAX and a JSP view built with the same data when accessed by a browser. 我之前使用过这种方法,当请求是通过AJAX返回JSON时,在浏览器访问时使用相同数据构建的JSP视图。
Jersey is pretty simple, works well, and serves as the reference implementation to boot. Jersey非常简单,效果很好,并且可以作为引导的参考实现。 Plus, it has some nice REST client support, much of which will probably make it into the JAX-RS spec. 此外,它有一些很好的REST客户端支持,其中很多可能会成为JAX-RS规范。
In terms of marrying that with Spring MVC, I'd recommend you make sure you model your application so that you have facades (Service classes) that provide all the core functionality you need and then simply reference them as needed in your MVC code or REST code. 在与Spring MVC结合使用方面,我建议您确保为应用程序建模,以便拥有提供所需核心功能的外观(服务类),然后根据需要在MVC代码或REST中引用它们码。 You shouldn't be duplicating business logic 您不应该重复业务逻辑
You can do this using Spring 3.0. 你可以使用Spring 3.0来做到这一点。 Spring 3.0 came out with the ability to specify @PathVariable
s to pull values out of the URL path (previously this was not easy in Spring MVC). Spring 3.0能够指定@PathVariable
s来从URL路径中提取值(以前在Spring MVC中这并不容易)。
You would also use @RequestMapping
to specify the HTTP method(s) each method in your controller should respond to. 您还可以使用@RequestMapping
指定控制器中每个方法应响应的HTTP方法。
I've also use Spring Security to implement an API key type of functionality. 我还使用Spring Security来实现API密钥类型的功能。 This way you can restrict access to your API in a way that is easy for REST clients to implement. 这样,您可以以易于REST客户端实现的方式限制对API的访问。 I had to extend org.springframework.web.filter.GenericFilterBean and add a the proper Authentication like this 我不得不扩展org.springframework.web.filter.GenericFilterBean并添加一个正确的身份验证,像这样
SecurityContextHolder.getContext().setAuthentication(apiKeyAuth)
There's a new REST API in Spring 3.0 MVC: Spring 3.0 MVC中有一个新的REST API:
http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/ http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/
http://www.springsource.org/download http://www.springsource.org/download
Apache CXF integrates well with Spring and offers many method for exposing services. Apache CXF与Spring集成良好,并提供了许多公开服务的方法。 From the overview section of the CXF homepage: 从CXF主页的概述部分:
CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. CXF使用前端编程API(如JAX-WS和JAX-RS)帮助您构建和开发服务。 These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI. 这些服务可以使用各种协议,例如SOAP,XML / HTTP,RESTful HTTP或CORBA,并可以在各种传输上工作,例如HTTP,JMS或JBI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.