问题
I have isntalled the latest version of jersey (Bundle-Version: 2.13.0) and the examples for that version. Then I tried (for testing Restful services - \examples\helloworld-pure-jax-rs\src\main\java\org\glassfish\jersey\examples) the Hello World example in Eclipse. The result ist this:
"Hello World" Jersey Example Application
Exception in thread "main" java.lang.IllegalArgumentException: No container provider supports the type interface com.sun.net.httpserver.HttpHandler
at org.glassfish.jersey.server.ContainerFactory.createContainer(ContainerFactory.java:87)
at org.glassfish.jersey.server.internal.RuntimeDelegateImpl.createEndpoint(RuntimeDelegateImpl.java:71)
at org.glassfish.jersey.examples.helloworld.jaxrs.App.startServer(App.java:72)
at org.glassfish.jersey.examples.helloworld.jaxrs.App.main(App.java:88)
I thought the example should work out of the box as it does not use any specific http servers. Only
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
My Java version is:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) Client VM (build 25.25-b02, mixed mode, sharing)
Any idea what could be wrong or what I missed?
best Klemens
回答1:
With Maven and with Eclipse
First I tried with Maven from command line (you need Maven installed). It worked fine.
Steps:
- Downloaded Jersey 2.13 Examples bundle from here
- Unzipped to
${myJerseyExampleLocation}(whatever the location may be) cd ${myJerseyExampleLocation}/jersey/examples/helloworld-pure-jax-rsmvn package- This downloaded all the dependencies and ran one unit testHelloWorldTestsuccessfullyTo run the main app
mvn exec:java. This runs theAppclass through the exec-maven-plugin listed in the<plugins>section of the pom. Result:Application started. Try accessing http://localhost:8080/helloworld in the browser. Hit enter to stop the application...- From browser to to
http://localhost:8080/helloworld. Result isHello World! - Go back to command line and hit enter to stop server
From Eclipse:
I first delete the entire unzipped example, as it was already built. I wanted to do it from scratch from Eclipse.
Steps:
- Unzipped
- From Eclipse
Import -> Maven -> Existing Maven ProjectsBrowse forhelloworld-pure-jax-rsand select select itFinish - Right click on project
Run As -> Maven Build. In the dialog in thegoalsfield typepackageTheApply -> Run. This will grab all the dependencies. You should get aBuild Successfulalong with a successful unit test. - Two options to run:
- Open the
Appclass, right click it andRun as -> Java Application. You should get the same result in the Eclipse console as mention the Maven Step 5. - Right click on project, select
Run as -> Maven Build(there are two select the one you haven't selected yet from the previous step). You will get the dialog again. This allows you to configure a different run configuration. In thegoalstypeexec:java. Then run. You should get same result as above.
- Open the
In Eclipse environment, tested with 1.7.0_65 and 1.8.0_20
Hopefully you can get it to work. Let me know what you come up with.
回答2:
In my case I was following an example. Finally I only needed to add this dependency org.glassfish.jersey.containers jersey-container-jdk-http (remember the exception's message "No container provider supports the type interface com.sun.net.httpserver.HttpHandler"):
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.18</version>
</dependency>
You could change the version according your environment.
来源:https://stackoverflow.com/questions/26542268/latest-jersey-example-does-not-work