Spring整合CXF webservice restful实例详解

网友投稿 333 2023-04-22


Spring整合CXF webservice restful实例详解

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

用到的基础类

User.java

@XmlRootElement(name="User")

public class User {http://

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://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整合配置

xmlns:p="http://springframework.org/schema/p"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:jaxrs="http://cxf.apache.org/jaxrs"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

xmlns:p="http://springframework.org/schema/p"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:jaxrs="http://cxf.apache.org/jaxrs"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

客户端调用示例:

对于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);

}

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

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;

}

到这里,就介绍完了,大家手动去操作一下吧,有问题大家一块交流。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:dubbo接口测试代码(java代码测试dubbo接口)
下一篇:完美解决java读取大文件内存溢出的问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~