资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在Spring中利用webservicerestful对CXF进行整合

今天就跟大家聊聊有关如何在Spring中利用webservice restful对CXF 进行整合,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联云计算的互联网服务提供商,拥有超过13年的服务器租用、四川雅安服务器托管、云服务器、虚拟空间、网站系统开发经验,已先后获得国家工业和信息化部颁发的互联网数据中心业务许可证。专业提供云主机、虚拟空间、国际域名空间、VPS主机、云服务器、香港云服务器、免备案服务器等。

webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了。

用到的基础类

User.java

@XmlRootElement(name="User")
public class User {

  private String userName;
  private String sex;
  private int age;
  
  public User(String userName, String sex, int age) {
    super();
    this.userName = userName;
    this.sex = sex;
    this.age = age;
  }
  
  public User() {
    super();
  }

  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
  public static void main(String[] args) throws IOException {
    System.setProperty("http.proxySet", "true"); 

    System.setProperty("http.proxyHost", "192.168.1.20"); 

    System.setProperty("http.proxyPort", "8080");
    
    URL url = new URL("http://www.baidu.com"); 

    URLConnection con =url.openConnection(); 
    
    System.out.println(con);
  }
}

接下来是服务提供类,PhopuRestfulService.java

@Path("/phopuService")
public class PhopuRestfulService {


  Logger logger = Logger.getLogger(PhopuRestfulServiceImpl.class);

  @GET
  @Produces(MediaType.APPLICATION_JSON) //指定返回数据的类型 json字符串
  //@Consumes(MediaType.TEXT_PLAIN) //指定请求数据的类型 文本字符串
  @Path("/getUser/{userId}")
  public User getUser(@PathParam("userId")String userId) {
    this.logger.info("Call getUser() method...."+userId);
    User user = new User();
    user.setUserName("中文");
    user.setAge(26);
    user.setSex("m");
    return user;
  }

  @POST
  @Produces(MediaType.APPLICATION_JSON) //指定返回数据的类型 json字符串
  //@Consumes(MediaType.TEXT_PLAIN) //指定请求数据的类型 文本字符串
  @Path("/getUserPost")
  public User getUserPost(String userId) {
    this.logger.info("Call getUserPost() method...."+userId);
    User user = new User();
    user.setUserName("中文");
    user.setAge(26);
    user.setSex("m");
    return user;
  }
}

web.xml配置,跟soap协议的接口一样


  
    cxf-phopu
    org.apache.cxf.transport.servlet.CXFServlet
  
  
    cxf-phopu
    /services/*
  

Spring整合配置



  
  
  

  
  
  
  
    
      
    
    
    
      
    
    
    
      
      
      
    
    
    
      
      
    
  
  

客户端调用示例:

对于get方式的服务,直接在浏览器中输入http://localhost:8080/phopu/services/phopuService/getUser/101010500 就可以直接看到返回的json字符串

{"userName":"中文","sex":"m","age":26} 

客户端调用代码如下:

public static void getWeatherPostTest() throws Exception{
    String url = "http://localhost:8080/phopu/services/phopuService/getUserPost";
    HttpClient httpClient = HttpClients.createSystem();
    //HttpGet httpGet = new HttpGet(url); //接口get请求,post not allowed
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader(CONTENT_TYPE_NAME, "text/plain");
    StringEntity se = new StringEntity("101010500");
    se.setContentType("text/plain");
    httpPost.setEntity(se);
    HttpResponse response = httpClient.execute(httpPost);

    int status = response.getStatusLine().getStatusCode();
    log.info("[接口返回状态吗] : " + status);

    String weatherInfo = ClientUtil.getReturnStr(response);

    log.info("[接口返回信息] : " + weatherInfo);
  }

客户端调用返回信息如下:

如何在Spring中利用webservice restful对CXF 进行整合

ClientUtil类是我自己封装的一个读取response返回信息的类,encoding是UTF-8

public static String getReturnStr(HttpResponse response) throws Exception {
    String result = null;
    BufferedInputStream buffer = new BufferedInputStream(response.getEntity().getContent());
    byte[] bytes = new byte[1024];
    int line = 0;
    StringBuilder builder = new StringBuilder();
    while ((line = buffer.read(bytes)) != -1) {
      builder.append(new String(bytes, 0, line, HTTP_SERVER_ENCODING));
    }
    result = builder.toString();
    return result;
  }

看完上述内容,你们对如何在Spring中利用webservice restful对CXF 进行整合有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


分享题目:如何在Spring中利用webservicerestful对CXF进行整合
链接地址:http://cdkjz.cn/article/jshicg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220