java學習

          java學習

           

          jersey面向資源開發1

          1.新建一個實體類
          package com.example.domain;
          import javax.xml.bind.annotation.XmlAttribute;
          import javax.xml.bind.annotation.XmlRootElement;
          @XmlRootElement(name = "device")
          public class Device {
          private String deviceIp;
          private int deviceStatus;
          public Device() {
          }
          public Device(String deviceIp) {
          super();
          this.deviceIp = deviceIp;
          }
          @XmlAttribute
          public String getIp() {
          return deviceIp;
          }
          public void setIp(String deviceIp) {
          this.deviceIp = deviceIp;
          }
          @XmlAttribute
          public int getStatus() {
          return deviceStatus;
          }
          public void setStatus(int deviceStatus) {
          this.deviceStatus = deviceStatus;
          }
          }
          其中@XmlRootElement(name = "device")代表xml文件的根節點
          @XmlAttribute代表xml文件的屬性節點
          在pom.xml文件中依賴下面2包
          <dependencies>
                  <dependency>
                      <groupId>org.glassfish.jersey.containers</groupId>
                      <artifactId>jersey-container-grizzly2-http</artifactId>
                  </dependency>
                  <!--  get JSON support: -->
                   <dependency>
                      <groupId>org.glassfish.jersey.media</groupId>
                      <artifactId>jersey-media-moxy</artifactId>
                  </dependency>
                 
                  <dependency>
                      <groupId>junit</groupId>
                      <artifactId>junit</artifactId>
                      <version>4.9</version>
                      <scope>test</scope>
                  </dependency>
              </dependencies>
          在服務類中編寫
          package com.example;
          import javax.ws.rs.GET;
          import javax.ws.rs.Path;
          import javax.ws.rs.Produces;
          import javax.ws.rs.core.MediaType;
          import com.example.domain.Device;
          @Path("myresource")
          public class MyResource {
          @GET
           @Produces({ MediaType.APPLICATION_JSON })
          public Device getIt() {
          Device d=new Device("1.1.1.1");
          d.setStatus(2);
          return d;
          }
          }
          建立測試類:
          package com.example;
          import java.io.IOException;
          import java.net.URI;
          import org.glassfish.grizzly.http.server.HttpServer;
          import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
          import org.glassfish.jersey.server.ResourceConfig;
          public class Main {
          public static final String BASE_URI = "http://localhost:8080/myapp/";
          public static HttpServer startServer() {
          final ResourceConfig rc = new ResourceConfig().packages("com.example");
          return GrizzlyHttpServerFactory.createHttpServer(URI.create(Main.BASE_URI), rc);
          }
          public static void main(String[] args) throws IOException {
          final HttpServer server = Main.startServer();
          }
          }
          啟動后在瀏覽器中輸入http://localhost:8080/myapp/myresource,頁面返回
          {"ip":"1.1.1.1","status":2}
          返回json格式的數據。

          posted on 2017-07-26 11:38 楊軍威 閱讀(151) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 辽宁省| 福泉市| 肥城市| 阿鲁科尔沁旗| 朝阳区| 蒲城县| 玉环县| 神木县| 赤峰市| 长寿区| 顺平县| 安乡县| 肃南| 明光市| 沙坪坝区| 庄浪县| 杭锦旗| 辛集市| 浙江省| 威海市| 鄢陵县| 罗山县| 高邮市| 奈曼旗| 廊坊市| 万山特区| 陇西县| 玉林市| 靖安县| 伊宁县| 保山市| 平潭县| 望谟县| 磐石市| 旬邑县| 辽宁省| 平和县| 花垣县| 肃北| 津南区| 克东县|