基于CXF的WebService开发介绍

Stupid 贡献于2012-05-10

作者 William  创建于2011-04-08 02:10:00   修改者William  修改于2011-04-08 07:02:00字数8137

文档摘要:基于CXF的WebService开发介绍简介下面用helloworld作为例子给大家介绍一下Cxf下webservice的开发过程本例中把接口即发布成Rest也发布成Webservice,大家自己开发过程中选择其一即可区分webservice和rest红色:webservice蓝色:rest源代码在附件中服务器端开发。
关键词:

基于CXF的WebService开发介绍 简介 Ø 下面用helloworld作为例子给大家介绍一下Cxf下webservice的开发过程 Ø 本例中把接口即发布成Rest也发布成Webservice,大家自己开发过程中选择其一即可 Ø 区分webservice和rest 红色:webservice 蓝色:rest Ø 源代码在附件中 服务器端开发 1.定义基本数据类型 package com.jd.ws; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class HelloWorld { private String message; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 2定义接口 package com.jd.ws; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.jws.WebService; import javax.jws.WebMethod; @SuppressWarnings("restriction") @WebService(serviceName = "hwws", name = "hwws", targetNamespace = "http://com.jd.ws") public interface HelloWorldService { @WebMethod @GET @Path("/sayHello/{name}") @Produces({MediaType.APPLICATION_JSON}) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) HelloWorld sayHello(@PathParam("name") String name); @WebMethod @POST @Path("/save/") @Produces("text/plain") @Consumes(MediaType.APPLICATION_JSON) String saveHello(HelloWorld hello); } 3.实现接口 package com.jd.ws; import java.util.ArrayList; import java.util.List; public class HelloWorldServiceImpl implements HelloWorldService{ public static List list=new ArrayList(); public HelloWorld sayHello(String name) { HelloWorld hw=new HelloWorld(); hw.setName(name); hw.setMessage("hi "+name); return hw; } public String saveHello(HelloWorld hello) { list.add(hello); System.out.println(hello.getMessage()); return "count:"+list.size()+";"+hello.getMessage(); } } 4.spring配置 5.web.xml配置 CXFServlet org.apache.cxf.transport.servlet.CXFServlet 5 CXFServlet /services/* 6.依赖包 org.apache.cxf cxf-rt-frontend-jaxws 2.3.3 org.apache.cxf cxf-rt-transports-http 2.3.3 org.apache.cxf cxf-rt-transports-http-jetty 2.3.3 org.apache.cxf cxf-rt-frontend-jaxrs 2.3.3 org.apache.cxf cxf-rt-bindings-http 2.3.3 commons-httpclient commons-httpclient 3.1 javax.ws.rs jsr311-api 1.1.1 org.codehaus.jettison jettison 1.2 客户端开发 1.代码实现 package com.jd.test; import java.net.URLEncoder; package com.jd.test; import java.net.URLEncoder; import junit.framework.TestCase; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.junit.Test; import org.codehaus.jackson.map.ObjectMapper; import com.jd.ws.HelloWorld; public class RestClientTest extends TestCase{ ObjectMapper mapper = new ObjectMapper();//可做成单例 @Test public void testGetHelloWorld() throws Exception { HttpClient httpclient = new DefaultHttpClient(); String url = "http://localhost:8080/warTest/services/helloworld/sayHello/"; String context = URLEncoder.encode("京东商城", "UTF-8"); HttpGet httpget = new HttpGet(url + context); HttpResponse response = httpclient.execute(httpget); String json=EntityUtils.toString(response.getEntity(), "utf-8"); //把json转成强类型的对象 HelloWorld hw=JSONUtil.toRestObject(json, HelloWorld.class); System.out.println(hw.getMessage()); } @Test public void testPostHellowWorld() throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://localhost:8080/warTest/services/helloworld/save/"); //String context = "hi 京东商城京东商城"; HelloWorld hw=new HelloWorld(); hw.setName("京东商城"); hw.setMessage("hi 京东商城"); String json=JSONUtil.toRestString(hw); StringEntity myEntity = new StringEntity(json, "application/json", "UTF-8"); httppost.setEntity(myEntity); HttpResponse response = httpclient.execute(httppost); System.out.println(EntityUtils.toString(response.getEntity(), "utf-8")); } } JSON封装的处理类 package com.jd.test; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import com.jd.ws.HelloWorld; public class JSONUtil { public static void main(String[] args) { try { // HelloWorld hw=toRestObject( // "{\"helloWorld\":{\"message\":\"hi 京东商城\",\"name\":\"京东商城\"}}", // HelloWorld.class); HelloWorld hw = new HelloWorld(); hw.setName("京东商城"); hw.setMessage("hi 京东商城"); System.out.println(toRestString(hw)); } catch (Exception e) { e.printStackTrace(); } } /** * 把字符串生成Rest的object,CXF rest JSON多了个头,需要先去掉再生成对象 * * @param * @param content * @param valueType * @return * @throws Exception */ public static T toRestObject(String content, Class valueType) throws Exception { ObjectMapper mapper = new ObjectMapper();// 可做成单例 String className = getShortClassName(valueType); JsonNode node = mapper.readValue(content, JsonNode.class); if (node == null) return mapper.readValue(content, valueType); else { node = node.get(className); return mapper.readValue(node.toString(), valueType); } } /** * 对象生成Rest的json串的话,需要加个头 * * @param obj * @return * @throws Exception */ public static String toRestString(Object obj) throws Exception { ObjectMapper mapper = new ObjectMapper();// 可做成单例 String json = mapper.writeValueAsString(obj); String className = getShortClassName(obj.getClass()); return "{\"" + className + "\":" + json + "}"; } private static String getShortClassName(Class valueType) { String className = valueType.getSimpleName(); className = className.substring(0, 1).toLowerCase() + className.substring(1, className.length()); return className; } } 2.依赖包 org.apache.httpcomponents httpclient 4.1.1 test org.codehaus.jackson jackson-core-asl 1.7.5 test org.codehaus.jackson jackson-mapper-asl 1.7.5 test 附件 这是我写的例子的源代码,里面也含有thrift的例子有兴趣的也可以看看

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档