j2ee培训:如何构建RESTful Web Service

时间:2020-10-09 12:04:24 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数据12-10

如何使用Web Service传输文件12-10

如何创建安全的Web Service12-10

C#开发和调用Web Service实例12-06

Web Service的开发与应用基础12-06

j2ee与java web的区别11-05

J2EE Web考试试题及答案09-17

Java Web开发和J2EE的区别11-11

如何面试Web前端开发12-12