RestFul风格

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。


应用实例

步骤

  1. 建工程添jar包(Cxf的jar包)
  2. 建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 + "]";
    }   
    
    
    }
    
  3. 建CustomerService接口并添加Restful风格相关的注释
  4. 编写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;
    
    }
    

    }

  5. 编写MainServer类,启动Restful的Webservice 启动后注意目前用rest而不是soap了,所以没有WSDL的描述了

  6. 浏览器地址栏里面按照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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值