<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • j2ee培訓(xùn):如何構(gòu)建RESTful Web Service

    時(shí)間:2024-07-03 11:01:09 J2EE培訓(xùn) 我要投稿
    • 相關(guān)推薦

    j2ee培訓(xùn):如何構(gòu)建RESTful Web Service

      JavaEE的核心是EJB3.0, 其提供了更兼便捷的企業(yè)級(jí)的應(yīng)用框架。下面yjbys小編為大家準(zhǔn)備了關(guān)于如何構(gòu)建RESTful Web Service的文章,歡迎閱讀。

    j2ee培訓(xùn):如何構(gòu)建RESTful Web Service

      1. 首先是實(shí)體類(lèi),注意其中的@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接口類(lèi),可以通過(guò)修改@Produces注解來(lái)聲明暴露接口返回的json還是xml數(shù)據(jù)格式

      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接口實(shí)現(xiàn)類(lèi)

      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.測(cè)試

      首先運(yùn)行MyServer類(lèi),然后運(yùn)行MyClient類(lèi)來(lái)驗(yàn)證Web Service。

    【j2ee培訓(xùn):如何構(gòu)建RESTful Web Service】相關(guān)文章:

    Web Service的開(kāi)發(fā)與應(yīng)用基礎(chǔ)07-12

    j2ee與java web的區(qū)別09-19

    Java Web開(kāi)發(fā)和J2EE的區(qū)別07-07

    構(gòu)建高性能J2EE應(yīng)用的技巧07-20

    J2EE Web架構(gòu)與CS架構(gòu)命名上的差異10-04

    如何面試Web前端開(kāi)發(fā)10-10

    我打算如何學(xué)習(xí)J2EE09-13

    如何由淺入深實(shí)踐學(xué)習(xí) Web 標(biāo)準(zhǔn)10-10

    J2EE架構(gòu)與設(shè)計(jì)培訓(xùn)08-30

    如何構(gòu)建基金銷(xiāo)售團(tuán)隊(duì)10-13

    主站蜘蛛池模板: 亚洲精品无码成人片在线观看| 亚洲国产精品自在拍在线播放| 精品多毛少妇人妻AV免费久久| 久久精品国产亚洲av水果派| 久久se精品一区精品二区国产| 国产亚洲欧洲精品| 亚洲欧美精品综合中文字幕| 国产精品人人做人人爽| 国产精品久久久久久| 久久久免费精品re6| 中文精品人人永久免费| 狠狠精品久久久无码中文字幕| 色播精品免费小视频| 国产成人亚洲精品| 国产三级久久久精品麻豆三级 | 老司机午夜精品视频资源| 青青青青久久精品国产h久久精品五福影院1421| 天天爽夜夜爽精品视频app| www.99精品| 国产suv精品一区二区33| 精品久久久久香蕉网| 久久精品亚洲精品国产色婷| 日产欧美国产日韩精品| 无码人妻精品一区二区在线视频| 中文成人无字幕乱码精品区| 日本精品一区二区三区在线视频| 精品亚洲视频在线观看| 国产精品自在在线午夜福利| 国产精品国产三级国产| 国产精品福利区一区二区三区四区| 国产av无码专区亚洲国产精品| 亚洲精品高清久久| 国产福利精品在线观看| 国产精品无码DVD在线观看| 国产福利精品视频自拍 | 国产在线观看高清精品| 国产香蕉国产精品偷在线观看| 国产精品免费一区二区三区| 国产精品欧美亚洲韩国日本久久| 国产精品亚洲不卡一区二区三区| 国产在线观看一区精品|