WebService问题

yaku2688 2011-08-19 11:24:45
根据协议开发WebService,如下:
请求报文:
<?xml version="1.0" encoding="UTF-8"?>
<STATUS>
<Header>
<ReqCode>001</ReqCode>
<ReqTime>20110801101010</ReqTime>
</Header>
<Body>
...
</Body>
</STATUS>
响应报文:
<?xml version="1.0" encoding="UTF-8"?>
<STATUS>
<Header>
<ReqCode>001</ReqCode>
<RspCode>000000</RspCode>
<RspDesc>成功</RspDesc>
<RspTime>20110801101010</RspTime>
</Header>
<Body>
...
</Body>
</STATUS>

如何根据协议开发呢?对WebService不熟悉,或者大家帮忙给点相关资料或者例子,多谢。
...全文
147 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaku2688 2011-08-19
  • 打赏
  • 举报
回复
就是怎么根据这个格式给他返回呢?WebService方式
softroad 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flyoversky 的回复:]

怎么感觉像是 http + xml
[/Quote]

SOAP协议了解下
flyoversky 2011-08-19
  • 打赏
  • 举报
回复
怎么感觉像是 http + xml
paneyjiang 2011-08-19
  • 打赏
  • 举报
回复
给你个示例吧

Cxf简单示例
下载jar包
http://cxf.apache.org
1.接口
package com.cxf.service;

import javax.jws.WebService;

@WebService
public interface HelloWord
{
String sayHi(String text);

}
2.实现类
package com.cxf.service;

import javax.jws.WebService;

@WebService(endpointInterface = "com.cxf.service.HelloWord")


public class HelloWordImpl implements HelloWord
{

public String sayHi(String text){
return "Hi" + text;
}


}
3.web.xml配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>
4.服务发布,需先写一个beans.xml(这里配置的是服务)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:endpoint
id="helloWorld"
implementor="com.cxf.service.HelloWordImpl"
address="http://localhost:8080/HelloWorld"/>

</beans>
5.如果通过spring结合方式访问还需要配置一个客户端文件(applications.xml)放在src下面
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


<jaxws:client id="helloClient"
serviceClass="com.cxf.service.HelloWord"
address="http://localhost:8080/HelloWorld" />

</beans>
6.发布
启动tomact,在浏览器输入http://localhost:8080/HelloWorld?wsdl
看是否能看到wsdl文件
7.测试

public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // your Spring ApplicationContext
HelloWord client = (HelloWord) context.getBean("helloClient");
String response = client.sayHi("jianglh");
System.out.println(response);

}


通过cxf 提供的客户端代理工厂类来访问

public static void main(String[] args)
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWord.class);
factory.setAddress("http://localhost:8080/HelloWorld");
HelloWord iHelloWorld = (HelloWord) factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:" + iHelloWorld.sayHi("Josen"));
//System.exit(0);
}
通过另外一种方式访问
private static final QName SERVICE_NAME = new QName("http://service.cxf.com/","HelloWord");
private static final QName PORT_NAME = new QName("http://service.cxf.com/","HelloWordPort");


public static void main(String[] args)
{

Service service =Service.create(SERVICE_NAME);
String endpointAddress="http://localhost:8080/HelloWorld";
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

HelloWord hi=service.getPort(HelloWord.class);
String returnvalue = hi.sayHi("hello");

System.out.println(returnvalue);


}

paneyjiang 2011-08-19
  • 打赏
  • 举报
回复
开发 webservice 有好几个架构,axis1.x, axis2,xfire,cxf,其中xfire比较好使(个人观点),cxf也行,现在xfire已经不再更新了,所以你可以选择cxf,官网上有很多例子
yaku2688 2011-08-19
  • 打赏
  • 举报
回复
XML做为请求参数并返回xml数据

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧