java Spring Restfull Jax-RS 注释支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25543692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Restfull Jax-RS annotation support
提问by Ahmet Karakaya
I have following class and web.xml files. Does spring framework supports jax-rs annotation such as @PATH
, and @PUT
, @Consumes
...etc.
我有以下类和 web.xml 文件。春天在什么框架支持JAX-RS注解,例如@PATH
,和@PUT
,@Consumes
...等。
In other word can I use @PATH
instead of @RequestMapping
换句话说,我可以使用@PATH
代替@RequestMapping
Java:
爪哇:
import org.springframework.stereotype.Controller;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@Controller
@Path("/register")
public class RegisterServices {
@PUT
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({"application/json"})
public Response create(@Context HttpServletRequest requestContex,
@HeaderParam("Authorization") String authorization,
String xMsisdn, String param) {}
}
web.xml
网页.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
采纳答案by Maleen Abewardana
No, you cannot use javax.ws.*
annotations in spring. You can use something like resteasy
with spring. It is rather easy. If you need I can provide with an example. (Jersey and CXF has good JAX-RS implementations too.)
不,您不能javax.ws.*
在 spring 中使用注释。你可以使用类似resteasy
spring 的东西。这很容易。如果你需要,我可以提供一个例子。(Jersey 和 CXF 也有很好的 JAX-RS 实现。)
AFAIK Springsource has no idea to provide an implementation to JAX-RS. So if you want to use the features described in JAX-RS you will not get it directly from spring. But you can develop a rest web service using spring. That's a different story. A questionwas found on SO on that.
AFAIK Springsource 不知道为 JAX-RS 提供实现。因此,如果您想使用 JAX-RS 中描述的功能,您将无法直接从 spring 中获得它。但是您可以使用 spring 开发一个休息 Web 服务。那是一个不同的故事。在 SO 上发现了一个问题。
Update
更新
Depending on PaulNUK's answer below, I need to clarify my answer. End of the day rest is a specification, and someone needs to implement it in first place.
根据下面 PaulNUK 的回答,我需要澄清我的回答。一天结束的休息是一个规范,首先需要有人来实现它。
Question clearly asks, whether we can replace the annotations, you cant do it, unless you add an external dependency like Jersey to your classpath. That case implementation is provided by Jersey.
问题明确提出,我们是否可以替换注解,您不能这样做,除非您在类路径中添加像Jersey这样的外部依赖项。该案例实现由 Jersey 提供。
Therefore you would never be able to use spring implemented JAX-RS annotation ever.
因此,您永远无法使用 spring 实现的 JAX-RS 注释。
回答by PaulNUK
Just put your JAX-RS (I'm using Jersey 2) annotations on a class, annotate that class with @Component to make it a Spring bean, and you have full JAX-RS support with Spring dependency injection.
只需将您的 JAX-RS(我使用的是 Jersey 2)注释放在一个类上,使用 @Component 注释该类以使其成为 Spring bean,并且您拥有对 Spring 依赖项注入的完整 JAX-RS 支持。
So Spring hasn't reinvented the wheel by implementing JAX-RS itself, but integrates very easily with Jersey for example.
所以 Spring 并没有通过实现 JAX-RS 本身来重新发明轮子,而是很容易地与例如 Jersey 集成。
Here's a simple Spring boot example:
这是一个简单的 Spring Boot 示例:
http://spring.io/blog/2014/11/23/bootiful-java-ee-support-in-spring-boot-1-2
http://spring.io/blog/2014/11/23/bootiful-java-ee-support-in-spring-boot-1-2
回答by Ripu Daman
If you want to develop the webservices using only Spring framework then Spring provides Spring MVC. Spring MVC has its own set of annotations. For e.g. "@RequestMapping". Spring MVC doesn't even adhere to the JAX-RS principles.
如果您只想使用 Spring 框架开发 Web 服务,那么 Spring 提供了 Spring MVC。Spring MVC 有自己的一组注解。例如“@RequestMapping”。Spring MVC 甚至不遵守 JAX-RS 原则。
There are various opensource frameworks (like Jersey) which supports "JAX-RS" and can be integrated with Spring.
有各种开源框架(如 Jersey)支持“JAX-RS”并且可以与 Spring 集成。
However, just in case you want to compare the Spring MVC with Jersey then below is the comparison. I personally support Jersey over SPring MVC as Spring MVC is not originally meant for webservices but for UI application.
但是,如果您想将 Spring MVC 与 Jersey 进行比较,那么下面是比较。我个人支持 Jersey 而不是 SPring MVC,因为 Spring MVC 最初不是用于 Web 服务而是用于 UI 应用程序。
- Same relative paths in multiple @Controllers not supported
- @ExceptionHandler is controller-centric
- Standard content negotiation can't respond with a fixed response type (SPR-6937)
- JSR 303 bean validation not applied in @Controllers (SPR-6928, scheduled for 3.1)
- Formatting responses (i.e. Date) not working using Spring formatter annotations
- You cannot return a bean from an exception handler and expect it to be automatically serialized to xml or json based on the incoming accept header.
- 不支持多个@Controller 中的相同相对路径
- @ExceptionHandler 以控制器为中心
- 标准内容协商无法以固定响应类型进行响应 (SPR-6937)
- JSR 303 bean 验证未应用于 @Controllers(SPR-6928,计划于 3.1)
- 使用 Spring 格式化程序注释格式化响应(即日期)不起作用
- 您不能从异常处理程序返回 bean 并期望它根据传入的接受标头自动序列化为 xml 或 json。
HTH...
哈...