Apache CXF中如何构建RESTful Web Service
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Apache CXF中如何构建RESTful Web Service,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。介绍一
千家信息网最后更新 2026年11月01日Apache CXF中如何构建RESTful Web Service
Apache CXF中如何构建RESTful Web Service,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
介绍一下怎么通过CXF来发布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。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
接口
代码
注解
帮助
运行
清楚
内容
可以通过
实体
对此
数据
文章
新手
更多
格式
知识
行业
资讯
资讯频道
还是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
花魁咖啡
互联网巨头科技公司
万易互联网科技
致聘互联网科技
银优互联网科技有限公司
互联网女皇 腾讯科技
互联网公司会议桌创意科技软装饰
顶级互联网科技成果有哪些
移动互联网科技股
互联网科技与管理
苏州五指互联网络科技有限
中国互联网十大科技股排名
工业互联网山东科技大学
黑云互联网科技有限公司
互联网营销推广黑科技
宇信科技在杭州互联网排第几
互联网科技包装股份有限公司上市
东营紫格互联网科技
广州智享互联网科技
互联网科技博主张洲
互联网科技媒体
沃里互联网科技
互联网科技股有哪些
创业天下互联网科技有限公司
世界互联网科技公司市值排名
互联网科技研发水平
济南有互联网科技公司吗
互联网相关科技技术
深圳互联网科技园地址
六子台科技互联网展会