WebServiceCXF框架的使⽤(实现JSON与XML数据传输)所依赖的jar包:apache-cxf-2.7.10,可以再官⽹下载,还有json.rar的对应包,联系是⼀个java 项⽬
⼀:下载对应Jar,可以在官⽹下载,或是到我资源下载,⾥⾯jar都齐全了,将apache-cxf-2.7.10下⾯的Lib中的jar加⼊项⽬中
⼆:.编写web services的服务端:需要⼀个接⼝,叫做Hello,代码如下:在类名称前加⼊@WebService注释
package com;
import javax.jws.WebService;
import net.sf.json.JSONObject;
@WebService
public interface Hello {
public String sayHello(String str);
public JSONObject sayJson(String str);
public String sayJsonStr(String str);
}
三:编写接⼝实现类:包括传递的两种参数,⼀种是拼接的xml字符串,⼀种是json的对象
package com;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
public class HelloImpl implements Hello {
@Override
public String sayHello(String str) {
System.out.println("web services调⽤成功");
String result = createXml(str);
return result;
}
public static void main(String[] args) throws Exception {
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "liqineng");
String[] likes = new String[] { "music", "movie", "study" };
jsonObj.put("hobby", likes);
Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("age", "23");
ingredients.put("EnglishName", "spider man");
ingredients.put("sex", "boy");
ingredients.put("love", "tangwei");
jsonObj.put("message", ingredients);
System.out.println(jsonObj);
}
/**
* 创建JsonOBject对象
*
* @param客户端传递的参数Name
* @return返回jsonObject对象
*/
public JSONObject createJsonObject(String name) {
public JSONObject createJsonObject(String name) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "liqineng");
String[] likes = new String[] { "music", "movie", "study" };
jsonObj.put("hobby", likes);
Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("age", "23");
ingredients.put("EnglishName", "spider man");
ingredients.put("sex", "boy");
ingredients.put("love", "tangwei");
jsonObj.put("message", ingredients);
return jsonObj;
}
/**
* 根据传递的字符串⽣成对应的xml。
*
* @param传递过来的参数name
* @return返回xml的字符串
*/
public String createXml(String name) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<Result>");
调用webservice服务sb.append("<cinamaName>机械战警</cinamaName>");
sb.append("<director>" + name + "</director>");
sb.append("<introduce>⼀部好莱坞⼤⽚,3D观影,不错</introduce>");
sb.append("<price>25</price>");
sb.append("</Result>");
String();
}
@Override
public JSONObject sayJson(String str) {
return createJsonObject(str);
}
@Override
public String sayJsonStr(String str) {
return createJsonObject(str).toString();
}
}
四:编写web services的启动类,代码如下,运⾏,web services即可查询了:
package com;
import f.endpoint.Server;
import f.jaxws.JaxWsServerFactoryBean;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setAddress("192.168.10.197:8080/cxf");
factory.setServiceClass(HelloImpl.class);
Server server = ate();
server.start();
}
}
然后可以输⼊:192.168.10.197:8080/cxf?wsdl 在浏览器上进⾏查看,你可以可以下载⼀个soap
Ui进⾏专业的测试第五:编写Webservice的客户端:如果不在同⼀个项⽬中的话,同样也需要下载对应的jar包⽂件,
需要创建⼀个接⼝,接⼝也是⽅法,注意:⽅法也需要添加@WebService注释,代码如下:package com;
import javax.jws.WebService;
import net.sf.json.JSONObject;
@WebService
public interface Hello {
public String sayHello(String str);
public JSONObject sayJson(String str);
public String sayJsonStr(String str);
}
主的接收和解析类:包括xml解析,json的解析:
package com;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
import net.sf.json.JSONObject;
import f.jaxws.JaxWsProxyFactoryBean;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
l.sax.InputSource;
l.sax.SAXException;
/**
* 客户端调⽤类。
*
*
*/
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("192.168.10.197:8080/cxf?wsdl");
Hello hello = (Hello) ate();
String xml = hello.sayHello("zhangsan");
// JSONObject obj = hello.sayJson("liqineng");
// parserXml(xml);
String str = hello.sayJsonStr("liqineng");
ParseJsonStr(str);
// System.out.println(xml);
}
/
**
* 解析xml字符串
*
* @param str传递过来的xml字符串
*/
public static void parserXml(String str) {
try {
StringReader read = new StringReader(str);
// 创建新的输⼊源SAX 解析器将使⽤ InputSource 对象来确定如何读取 XML 输⼊ InputSource source = new InputSource(read);
// 创建⼀个新的SAXBuilder
DocumentBuilderFactory dbf = wInstance();
DocumentBuilder db = wDocumentBuilder();
Document document = db.parse(source);
NodeList movies = ChildNodes();
for (int i = 0; i < Length(); i++) {
Node employee = movies.item(i);
NodeList employeeInfo = ChildNodes();
for (int j = 0; j < Length(); j++) {
Node node = employeeInfo.item(j);
if (NodeType() == Node.ELEMENT_NODE) {
if ("cinamaName".NodeName())) {
System.out.println("电影名称:" + TextContent());
} else if ("introduce".NodeName())) {
System.out.println("电影简介:" + TextContent());
} else if ("price".NodeName())) {
System.out.println("电影价格:" + TextContent());
} else if ("director".NodeName())) {
System.out.println("导演:" + TextContent());
}
}
}
}
} catch (FileNotFoundException e) {
System.out.Message());
} catch (ParserConfigurationException e) {
System.out.Message());
} catch (SAXException e) {
System.out.Message());
} catch (IOException e) {
System.out.Message());
}
}
public static void ParseJsonObject(JSONObject obj) {
System.out.println(obj);
String name = String("name");
}
/**
* 解析json字符串
*
* @param str传递过来的json字符串
*/
public static void ParseJsonStr(String str) {
JSONObject obj = JSONObject.fromObject(str);
System.out.println(obj);
String name = String("name");
String hobby = String("hobby");
JSONObject message = JSONObject("message");
String love = String("love");
String sex = String("sex");
int age = Int("age");
String englishName = String("EnglishName");
System.out.println(hobby);
System.out.println(name + ">>" + hobby + ">>" + love + ">>" + sex
+ ">>" + age + ">>" + englishName);
}
}
这样,⼀个java的Webservice完成了,并进⾏的现在⽐较流⾏的xml,json的解析,希望⼤家看了之后对⾃⼰有所帮助,如有疑问,留⾔讨论
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论