亚洲一级电影在线观看,九九精品无码专区免费,亚洲AV无码资源在线观看 ,欧美国产高清

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精品国产兔费观看久久99| 久草热久草热线频97精品| 99亚洲男女激情在线观看 | 康保县| 暴力调教一区二区三区| 亚洲国产制服丝袜先锋| 国产亚洲精aa在线看| 97色伦色在线综合视频| 欧美成人aaa片一区国产精品| 精品国产av色一区二区深夜久久 | 国产在线精品一区二区在线看| 精品人伦一区二区三区潘金莲| 雅安市| 色综合av综合无码综合网站| 777亚洲精品乱码久久久久久| 庆元县| 国产中文欧美日韩在线| 亚洲一区 日韩精品 中文字幕 | 国产网站在线看| 国产精品亚洲а∨无码播放麻豆| 国产成人精品免费视频app软件| 日韩在线中文| 一本一道中文字幕无码东京热| 色综合久久无码五十路人妻| 国产JIZzJIzz视频全部免费| 久久人人爽人人人人片av| 国产在线无码不卡播放| 久久综合五月婷婷| 国产偷抇久久精品a片蜜臀av| 国产精品自产拍在线18禁| 中文字幕无线乱码人妻| 91小视频在线观看| 国产9色在线 | 日韩| 武宣县| 国产成人a视频高清在线观看| 欧美饥渴熟妇高潮喷水水| 亚洲自拍另类| 欧美成人精品手机在线| 国产成人无码午夜视频在线观看| 天天爱天天做天天爽|