亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

J2EE培訓

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

時間:2024-06-21 00:25:56 J2EE培訓 我要投稿
  • 相關推薦

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

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

j2ee培訓:如何構(gòu)建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數(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接口實現(xiàn)類

  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培訓:如何構(gòu)建RESTful Web Service】相關文章:

Java Web開發(fā)和J2EE的區(qū)別08-29

如何面試Web前端開發(fā)06-21

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

如何識別和防御Web網(wǎng)頁木馬08-25

如何保證Web服務器安全07-10

如何構(gòu)建基金銷售團隊08-06

RESTful圖書管理系統(tǒng)架構(gòu)設計07-11

如何用Dreamweaver批量做web網(wǎng)頁詳細步驟07-25

Web2.0該如何走向商業(yè)化10-15

如何構(gòu)建個人核心競爭力08-10