Java类,封装为Web资源。
RestFul风格的get方式
➢ JAX-RS提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源。
@Path,标注资源类或者方法的相对路径
@GET,@PUT,@POST,@DELETE,标注方法是HTTP请求的类型。
@Produces,标注返回的MIME媒体类型
@Consumes,标注可接受请求的MIME媒体类型
content-type就是媒体类型,对应 的媒体类型=”text/html”
=”application/json”
=”application/xml”
=”application/x-www-form-urlencoded”【名值对的】对应的从后台向页面传输服务的时候,他用的是什么类型
@PathParam,@QueryParam,@HeaderParam,@CookieParam,
@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,
例如:
@PathParam来自于URL的路径,
@QueryParam来自于URL的查询参数,
@HeaderParam来自于HTTP请求的头信息,
@CookieParam来自于HTTP请求的Cookie。
应用实例
步骤:
- 建工程添jar包(Cxf的jar包)
建Customer.java的entity并添加注解@XmlRootElement
package com.atguigu.entity; importjavax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Customer { public Customer (){ } private String id ; private String name; private Integer age; public Customer(String id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
- 建CustomerService接口并添加Restful风格相关的注释
编写CustomerServiceImpl实现类
@Path("/crm") public class CustomerService { @Path("/customer/{id}") @GET @Produces("text/html") public Customer getCustomerByID(@PathParam("id") String id){ Customer customer = new Customer(id,"tian7",77); return customer; }
}
编写MainServer类,启动Restful的Webservice 启动后注意目前用rest而不是soap了,所以没有WSDL的描述了
浏览器地址栏里面按照Restful风格的路径进行访问+测试
public class MainService { public static void main(String[] args) { JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean(); jaxrsServerFactoryBean.setAddress("http://192.168.19.71:9999/custom"); jaxrsServerFactoryBean.setResourceClasses(CustomerService.class); jaxrsServerFactoryBean.create().start(); System.out.println("又开业了");
}
}
RestFul风格的Post方法
使用 HttpClient需要以下6个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例
3. 调用第一步中创建好的实例的execute方法来执行第二步中创建好的链接类
实例
4. 读response获取HttpEntity
5. 对得到后的内容进行处理
6. 释放连接。无论执行方法是否成功,都必须释放连接
➢ 客
package com.atguigu.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class MainPost {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//2. 创建某种连接方法的实例
HttpPost httpPost = new HttpPost("http://192.168.19.71:9999/custom/crm/customer");
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("id", "0101"));
list.add(new BasicNameValuePair("name", "哈哈"));//进行IO流转换的时候会出现乱码
list.add(new BasicNameValuePair("age", "18"));
HttpEntity entityPost = new UrlEncodedFormEntity(list,"utf8");//在这转换,请求
httpPost.setEntity(entityPost);
//3. 调用第一步中创建好的实例的execute方法来执行第二步中创建好的链接类实例
HttpResponse httpResponse = httpClient.execute(httpPost);
//4. 读response获取HttpEntity
HttpEntity entity = httpResponse.getEntity();
//5. 对得到后的内容进行处理
if(httpResponse.getStatusLine().getStatusCode() == org.eclipse.jetty.http.HttpStatus.OK_200){
System.out.println(EntityUtils.toString(entity,"utf8"));//IO流转换,响应
}else{
httpResponse.getStatusLine().toString();
}
//6. 释放连接。无论执行方法是否成功,都必须释放连接
EntityUtils.consume(entity);
httpClient.close();
}
}