简体   繁体   English

与Jersey和JSR相关的JAX-RS

[英]JAX-RS in relation to Jersey and JSRs

I'm trying to get my head around some concepts in Java: 我试图了解Java中的一些概念:

  1. JSR(s): describe specifications, but carry no actual implementations. JSR:描述规范,但没有实际实现。 Eg http://jsr311.java.net/ is the "home" for "Java™ API for RESTful Web Services". 例如, http://jsr311.java.net/是“用于RESTful Web服务的Java™API”的“主页”。 It serves as a common reference for all implementations of JSR-311. 它是JSR-311所有实现的通用参考。
  2. One can download the interfaces (?) of JSR-311 from http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api , however, unless you are implementing JSR-311 by yourself these have no particular value? 可以从http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api下载JSR-311的接口(?),但是,除非您自己实现JSR-311,否则它们没有特别的价值?
  3. JSR(s) will usually/always have a reference implementation. JSR通常/总是有一个参考实现。 To find it you'll have to google "JSR XXX reference implementation" or see the specifications home page (eg http://jsr311.java.net/ ) 要找到它,你必须google“JSR XXX参考实现”或查看规范主页(例如http://jsr311.java.net/
  4. For JSR-311 this reference implementation is Jersey . 对于JSR-311,这个参考实现是Jersey Using maven you can get the jersey server from http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9 . 使用maven,您可以从http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9获取泽西服务器。 Since Jersey provides an implementation according to the interfaces found in http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api , you only need to add Jersey as a dependency in your project and not the jsr311-api itself. 由于Jersey根据http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api中的接口提供了一个实现,因此您只需要在项目中添加Jersey作为依赖项,而不是jsr311-api本身。 (this applies to all JSR technologies?) (这适用于所有JSR技术?)
  5. Putting both http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api and http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9 as dependencies in your project will possibly cause classpath problems? http://mvnrepository.com/artifact/javax.ws.rs/jsr311-apihttp://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9作为项目中的依赖项放在一起导致类路径问题?

Am I completely off or onto someting? 我是完全不喜欢的吗?

  1. Yes, this isn't anything new. 是的,这不是什么新鲜事。 Think about JDBC, java provides the interfaces ( Connection , Statement , ResultSet etc) but it is up to database vendors to provide implementations. 想想JDBC,java提供了接口( ConnectionStatementResultSet等),但是由数据库供应商提供实现。

  2. If you're using a JSR-311 implementation like Jersey or Apache CXF then you'll annotate your classes with the javax.ws.rs annotations, such as @Path , @GET , @Produces etc. This is why you need to explicitly have JSR-311 as a maven dependency. 如果您正在使用像Jersey或Apache CXF这样的JSR-311实现,那么您将使用javax.ws.rs注释来注释您的类,例如@GET @Path@GET@GET @Produces等。这就是您需要明确的将JSR-311作为maven依赖。

  3. Yes, usually. 是的,通常。 Have a look at the JSR list on wiki . 看看wiki上的JSR列表

  4. You need both the JSR and the implementation. 您需要JSR和实现。 The annotations are in the JSR, the implementation provides supporting classes, such as com.sun.jersey.spi.container.servlet.ServletContainer . 注释在JSR中,实现提供了支持类,例如com.sun.jersey.spi.container.servlet.ServletContainer

  5. No, it is necessary to have both as dependencies (see point 4); 不,有必要兼顾两者(见第4点); you won't get classpath conflicts. 你不会得到类路径冲突。

  1. -
  2. One can download files from a variety of sources. 可以从各种来源下载文件。 To get the most official version of the JSR-311 specification go to its JCP download page . 要获得JSR-311规范的最正式版本,请转到其JCP下载页面 It's quite possible that you can't get a JAR file (with all the interfaces and stuff) from JCP pages, but still, this is the official source. 您很可能无法从JCP页面获取JAR文件(包含所有接口和内容),但这仍然是官方来源。 (There are always nice PDFs of public drafts also!) (总有很好的PDF公共草稿!)
  3. -
  4. You're right, because Jersey contains the API defined by JSR-311, however I would add a compile dependency to the jsr311-api JAR file and add Jersey as runtime dependency. 你是对的,因为Jersey包含JSR-311定义的API,但是我会在jsr311-api JAR文件中添加一个编译依赖项,并将Jersey添加为运行时依赖项。 This creates a nice separation between API and implementation and you can swap out your JSR-311 implementation anytime [ sic ]. 这在API和实现之间创建了一个很好的分离,你可以随时更换你的JSR-311实现[ 原文如此 ]。 If you intend to use Jersey all the way include only Jersey. 如果你打算使用泽西一路只包括新泽西州。 One less dependency in your POM. POM中的依赖性较少。
  5. If Jersey packages the same API as the jsr311-api JAR contains, it won't. 如果Jersey包含与jsr311-api JAR包含的API相同的API,则不会。 If it packages something different, well, that would be awful! 如果它包装了不同的东西,那么,那将是非常糟糕的! Maven will probably bark at compile time if one has a corrupt JSR-311 API on its classpath (I've already seen lots of java.lang.ClassFormatError: Absent Code attribute in method that ... errors, so it won't go unnoticed, that's for sure). 如果在其类路径上有一个损坏的 JSR-311 API,Maven可能会在编译时吠叫(我已经在方法中看到了很多java.lang.ClassFormatError:Absent Code属性......错误,所以它不会去没有注意到,这是肯定的)。

Other than these, you're right. 除此之外,你是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM