JAX-RS示例(Jersey)

我們可以通過 jersey 實現創建JAX-RS示例。 為此,需要加載 jersey相關jar檔或使用Maven框架。

在這個例子中,我們使用jersey jar檔來實現JAX-RS jersey示例。

Jersey Jar檔下載網址:https://jersey.github.io/download.html

打開Eclipse,創建一個Web工程: restfuljersey,如下圖所示 -

JAX-RS創建了4個檔示例:

  • Hello.java
  • web.xml
  • index.html
  • HelloWorldClient.java

3個檔是為伺服器端創建的,最後一個檔:HelloWorldClient.java 是為客戶端應用程式創建的。

JAX-RS伺服器代碼

檔:Hello.java -

package com.zaixian.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {
  // This method is called if HTML and XML is not requested
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey Plain";
  }
  // This method is called if XML is requested
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is requested
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey HTML" + "</h1></body>" + "</html> ";
  }
}

檔:web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.zaixian.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

檔:index.html -

<a href="rest/hello">點擊這裏查看</a>

現在在伺服器上運行此應用程式 這裏Tomcat伺服器是在端口8080上運行。專案名稱是restfuljersey

運行專案後,點擊上面鏈接,將看到以下輸出:

JAX-RS客戶端代碼

檔:ClientTest.java 在伺服器應用程式內創建,也可以通過服務介面和jersey jar檔來運行其他應用程式的客戶端代碼。

檔:ClientTest.java -

package com.zaixian.restclient;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class ClientTest {
  public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target(getBaseURI());
    //Now printing the server code of different media type
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class));
  }
  private static URI getBaseURI() {
    //here server is running on 8080 port number and project name is restfuljersey
    return UriBuilder.fromUri("http://localhost:8080/restfuljersey").build();
  }
}

執行上面客戶端代碼,得到以下結果 -

Hello Jersey Plain
<?xml version="1.0"?><hello> Hello Jersey</hello>
<html> <title>Hello Jersey</title><body><h1>Hello Jersey HTML</h1></body></html>

上一篇: JAX-RS教學 下一篇: RESTful JAX-RS注解示例