How to deploy a JAX-RS application in Java SE?

I want to encode a RESTful web service using JAX-RS, and I want to publish it to a local host, for example http://localhost:[port] . I read the following in this:

The Java SE 7 (JSR 336) and Java SE 8 (JSR 337) specifications do not include the JAX-RS component. However, JAX-RS applications can be published in Java SE environments (using RuntimeDelegate ) and JAX-RS implementations can also support publishing through JAX-WS.

Indicated by RuntimeDelegate . How can i use it? If there are good examples of how to achieve this, please share them with me.

+5
source share
2 answers

To deploy the JAX-RS application in Java SE, you can use the RuntimeDelegate and the HTTP server supported by your JAX -RS. A servlet container is not required.

JSR 339 states the following:

In Java SE, a configured instance of an endpoint class can be obtained using the createEndpoint RuntimeDelegate method. An application provides an instance of Application and the type of endpoint required. An implementation MAY support null or more finite types of any desired type.

How the resulting endpoint class instance is used to publish the application is beyond the scope of this specification.

Jersey, the JAX-RS reference implementation, supports a range of HTTP servers that can be used to deploy JAX-RS applications in Java SE.

For example, Grizzly and RuntimeDelegate , you can get the following:

 public class Example { public static void main(String[] args) { ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.register(GreetingsResource.class); HttpHandler handler = RuntimeDelegate.getInstance() .createEndpoint(resourceConfig, HttpHandler.class); HttpServer server = HttpServer.createSimpleServer(null, 8080); server.getServerConfiguration().addHttpHandler(handler); try { server.start(); System.out.println("Press any key to stop the server..."); System.in.read(); } catch (Exception e) { System.err.println(e); } } @Path("/greetings") public static class GreetingsResource { @GET @Produces(MediaType.TEXT_PLAIN) public String getGreeting(){ return "Hello from the other side."; } } } 

The application will be available at http://localhost:8080/greetings .

For the example shown above, the following dependencies are required:

 <dependency> <groupId>org.glassfish.grizzly</groupId> <artifactId>grizzly-http-server</artifactId> <version>2.3.30</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-grizzly2-http</artifactId> <version>2.25.1</version> </dependency> 

Other supported implementations include:

The Jersey documentation also describes other deployment options for a Java SE environment without RuntimeDelegate .

+4
source

To create a REST web service, you can not only use Java SE. I am mistaken, Cassio mentioned that JAX RS can only be implemented using Java SE. I suggested that you learn more about the differences between Java SE and Java EE, for example, in this discussion .

To answer your question, you can easily create a REST web service by simply adding the javaee 7 api dependency, and then deploy the web archive (war) project to the Application Server, for example, wildfly, payara or glashfish, and here is an example of how build a REST web service with Java EE 7 . Another way to create a REST web service with Spring Frameworks, and they provide a simple and good REST tutorial with Spring .

-1
source

Source: https://habr.com/ru/post/1266470/


All Articles