<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • j2ee培訓:如何構建RESTful Web Service

    時間:2024-07-03 11:01:09 J2EE培訓 我要投稿
    • 相關推薦

    j2ee培訓:如何構建RESTful Web Service

      JavaEE的核心是EJB3.0, 其提供了更兼便捷的企業級的應用框架。下面yjbys小編為大家準備了關于如何構建RESTful Web Service的文章,歡迎閱讀。

    j2ee培訓:如何構建RESTful Web Service

      1. 首先是實體類,注意其中的@XmlRootElement注解

      package com.googlecode.garbagecan.cxfstudy.jaxrs;

      import java.util.Date;

      import javax.xml.bind.annotation.XmlRootElement;

      @XmlRootElement(name="Customer")

      public class Customer {

      private String id;

      private String name;

      private Date birthday;

      public String getId() {

      return id;

      }

      public void setId(String id) {

      this.id = id;

      }

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public Date getBirthday() {

      return birthday;

      }

      public void setBirthday(Date birthday) {

      this.birthday = birthday;

      }

      @Override

      public String toString() {

      return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);

      }

      }

      2. RESTful Web Service接口類,可以通過修改@Produces注解來聲明暴露接口返回的json還是xml數據格式

      package com.googlecode.garbagecan.cxfstudy.jaxrs;

      import javax.ws.rs.GET;

      import javax.ws.rs.Path;

      import javax.ws.rs.PathParam;

      import javax.ws.rs.Produces;

      import javax.ws.rs.QueryParam;

      @Path(value = "/customer")

      @Produces("*/*")

      //@Produces("application/xml")

      //@Produces("application/json")

      public interface CustomerService {

      @GET

      @Path(value = "/{id}/info")

      Customer findCustomerById(@PathParam("id")String id);

      @GET

      @Path(value = "/search")

      Customer findCustomerByName(@QueryParam("name")String name);

      }

      3. RESTful Web Service接口實現類

      package com.googlecode.garbagecan.cxfstudy.jaxrs;

      import java.util.Calendar;

      public class CustomerServiceImpl implements CustomerService {

      public Customer findCustomerById(String id) {

      Customer customer = new Customer();

      customer.setId(id);

      customer.setName(id);

      customer.setBirthday(Calendar.getInstance().getTime());

      return customer;

      }

      public Customer findCustomerByName(String name) {

      Customer customer = new Customer();

      customer.setId(name);

      customer.setName(name);

      customer.setBirthday(Calendar.getInstance().getTime());

      return customer;

      }

      }

      4. Server端代碼

      package com.googlecode.garbagecan.cxfstudy.jaxrs;

      import org.apache.cxf.interceptor.LoggingInInterceptor;

      import org.apache.cxf.interceptor.LoggingOutInterceptor;

      import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

      public class MyServer {

      public static void main(String[] args) throws Exception {

      JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();

      factoryBean.getInInterceptors().add(new LoggingInInterceptor());

      factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

      factoryBean.setResourceClasses(CustomerServiceImpl.class);

      factoryBean.setAddress("http://localhost:9000/ws/jaxrs");

      factoryBean.create();

      }

      }

      5. Client端代碼

      package com.googlecode.garbagecan.cxfstudy.jaxrs;

      import org.apache.commons.httpclient.HttpClient;

      import org.apache.commons.httpclient.HttpStatus;

      import org.apache.commons.httpclient.methods.GetMethod;

      public class MyClient {

      public static void main(String[] args) throws Exception {

      go("http://localhost:9000/ws/jaxrs/customer/1/info");

      go("http://localhost:9000/ws/jaxrs/customer/search?name=abc");

      }

      private static void go(String url) throws Exception {

      HttpClient client = new HttpClient();

      GetMethod method = new GetMethod(url);

      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {

      System.err.println("Method failed: " + method.getStatusLine());

      }

      byte[] responseBody = method.getResponseBody();

      System.out.println(new String(responseBody));

      }

      }

      6.測試

      首先運行MyServer類,然后運行MyClient類來驗證Web Service。

    【j2ee培訓:如何構建RESTful Web Service】相關文章:

    Web Service的開發與應用基礎07-12

    j2ee與java web的區別09-19

    Java Web開發和J2EE的區別07-07

    構建高性能J2EE應用的技巧07-20

    J2EE Web架構與CS架構命名上的差異10-04

    如何面試Web前端開發10-10

    我打算如何學習J2EE09-13

    如何由淺入深實踐學習 Web 標準10-10

    J2EE架構與設計培訓08-30

    如何構建基金銷售團隊10-13

    主站蜘蛛池模板: 国产精品极品| 日韩精品人成在线播放| 国产午夜精品久久久久九九| 久久99精品久久久久子伦| 成人国内精品久久久久影院VR| 欧洲精品久久久av无码电影| 国产亚洲色婷婷久久99精品91 | 日本精品久久久中文字幕 | 国产午夜精品一本在线观看| 99精品高清视频一区二区| 无码人妻精品一区二区三区久久久 | 国产精品一区二区不卡| 老司机亚洲精品影院无码| 国产精品国产三级国产AV主播 | 精品国产一区AV天美传媒| 亚洲精品偷拍视频免费观看| 精品乱子伦一区二区三区高清免费播放 | 日韩欧精品无码视频无删节| 亚洲AV日韩精品一区二区三区| 国产精品麻豆欧美日韩ww| 久久青青草原国产精品免费| 国产精品视频白浆免费视频| 久久久精品2019免费观看| 亚洲精品中文字幕乱码三区| 狠狠色伊人久久精品综合网 | 99热亚洲精品6码| 久久综合九色综合精品| 久久精品一区二区国产| 欧美777精品久久久久网| 久久精品国产秦先生| 久久国产精品-国产精品| 久久成人国产精品二三区| 精品久久久久久亚洲| 久久国产精品-久久精品| 久久精品国产91久久综合麻豆自制| 国产91大片精品一区在线观看| 99精品国产在热久久无毒不卡| …久久精品99久久香蕉国产 | 国产三级久久久精品麻豆三级| 国产精品无码av在线播放| 国产麻豆精品久久一二三|