使用jax-ws开发webservice(二)

xiaoxiao2021-02-28  116

JAX-WS注解

WebService的注解都位于javax.jws包下:

@WebService-定义服务,在public class上边

targetNamespace:指定命名空间

name:portType的名称

portName:port的名称

serviceName:服务名称

endpointInterface:SEI接口地址,如果一个服务类实现了多个接口,只需要发布一个接口的方法,可通过此注解指定要发布服务的接口。

@WebMethod-定义方法,在公开方法上边

       operationName:方法名

       exclude:设置为true表示此方法不是webservice方法,反之则表示webservice方法

@WebResult-定义返回值,在方法返回值前边

       name:返回结果值的名称

@WebParam-定义参数,在方法参数前边

       name:指定参数的名称

作用:

通过注解,可以更加形像的描述Web服务。对自动生成的wsdl文档进行修改,为使用者提供一个更加清晰的wsdl文档。

当修改了WebService注解之后,会影响客户端生成的代码。调用的方法名和参数名也发生了变化

使用注解应注意:

@WebMethod对所有非静态的公共方法对外暴露为服务.

对于静态方法或非public方法是不可以使用@WebMethod注解的.

对public方法可以使用@WebMethod(exclude=true)定义为非对外暴露的服务。

使用jaxws规范开发webservice

服务端的编写

接口的编写: @WebService public interface IWeather { public String query(String cityName); } 接口实现类的编写: public class WeatherImpl implements IWeather{ public String query(String cityName) { return cityName+":晴天"; } }发布服务: public class test { public static void main(String[] args) { JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean(); jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/cxf_jaxws_server"); jaxWsServerFactoryBean.setServiceClass(IWeather.class); jaxWsServerFactoryBean.setServiceBean(new WeatherImpl()); jaxWsServerFactoryBean.create(); System.out.println("已经发布成功"); } }

客户端的编写:

获取服务实现类: wsimport -d . http://127.0.0.1:12345/cxf_spring_service 编写客户端代码 public class client { public static void main(String[] args) { JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/cxf_jaxws_server"); jaxWsProxyFactoryBean.setServiceClass(IWeather.class); IWeather i=jaxWsProxyFactoryBean.create(IWeather.class); System.out.println(i.query("北京")); } }

jaxws规范整合spring进行开发:

服务器端编写:

配置文件的编写

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>cxf_spring_service</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>aaa</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>aaa</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>ApplicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:server address="/cxf_spring_service" serviceClass="com.cn.aa.IWeather"> <jaxws:serviceBean> <ref bean="aex"/> </jaxws:serviceBean> </jaxws:server> <bean id="aex" class="com.cn.aa.WeatherImpl"></bean> </beans>

类实现

接口: @WebService public interface IWeather { public String query(String cityName); } 实现类: public class WeatherImpl implements IWeather{ public String query(String cityName) { return cityName+":晴天"; } }

客户端的编写

配置文件的编写

web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>cxf_spring_client</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>ApplicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:client id="xiaopin" address="http://127.0.0.1:8080/cxf_spring_service/ws/cxf_spring_service" serviceClass="com.cn.aa.IWeather"> </jaxws:client> </beans>

类实现

获取服务器实现类 public class text { public static void main(String[] args) { ApplicationContext aa=new ClassPathXmlApplicationContext("ApplicationContext.xml"); IWeather i=(IWeather)aa.getBean("xiaopin"); System.out.println(i.query("天津")); } }
转载请注明原文地址: https://www.6miu.com/read-60720.html

最新回复(0)