JAVA webservice实现总结

概述

首先web服务的方式常规来看主要有JAX-WS和JAX-RS:

  • JAX-WS全称是JavaTM API forXML-Based WebServices
  • JAX-RS全称是JavaTM APIforRESTful Web Services

JAX-WS是针对WebService。而JAX-RS是针对RESTful HTTP Service,可以说这是两种风格的SOA架构风格。在网上看到这么一句话:JAVA 中共有三种WebService 规范,分别是JAXM&SAAJ、JAX-WS(JAX-RPC)、JAX-RS。

JAX-WS规范是一组XML web services的JAVA API,JWS-WS为实现基于soap协议的Web Service提供的API,SOAP协议比较强大。而后者JWS-RS是为基于REST设计风格的WebServcice提供的API。

支持的框架:
支持JAX-WS服务规范的框架有:CXF,Axis,Xfire。结合java语言均可可实现JAX-WS
支持JAX-RS服务规范的框架有:

  1. CXF——XFire和Celtix的合并
  2. Jersey——Sun公司的JAX-RS参考实现。
  3. RESTEasy——JBoss的JAX-RS项目。
  4. Restlet——也许是最早的REST框架了,它JAX-RS之前就有了。

即JAX-RS用于restful格式的webservice,jax-ws支持soap格式的webservice。

实战

JAX-WS方式

示例一:

  1. java部分
*接口类
@WebService
public interface  HelloWorldDao {
    
    @WebMethod
    @WebResult(name = "result")
    public String sayHello();
}
*实现类
@WebService(name="HelloWorld", targetNamespace="http://csvalue.cn",
              endpointInterface = "io.raycom.modules.ws.demo.HelloWorldDao")
public class HelloWorldImpl implements HelloWorldDao {

       @Override
        public String sayHello() {
            return "helloWorld";
        }
}

2:xml部分

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-4.0.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-servlet.xml"/>    
    
    <bean id="helloWorldImpl" class="io.raycom.modules.ws.demo.HelloWorldImpl" />
    
    <!-- 
    <jaxws:endpoint id="helloworld" implementor="#helloWorldImpl" address="/helloworld" />
    -->
    <!-- 2:通过jaxws:server方式来配置webservice -->
    <jaxws:server serviceClass="io.raycom.modules.ws.demo.HelloWorldDao"
          address="/helloworld">
        <jaxws:serviceBean>
            <ref bean="helloWorldImpl" />
        </jaxws:serviceBean>
    </jaxws:server>    
</beans>

示例二:

  1. java部分
*接口类
@WebService
public interface Common {
    
    @WSDLDocumentation( "用户登陆")  
    String checkUser(@WebParam(name="empNo")String empNo, @WebParam(name="password")String password);

    String getEntity(@WebParam(name="empNo")String empNo, @WebParam(name="password")String password, 
            @WebParam(name="moduleName")String moduleName, @WebParam(name="paramsXml")String paramsXml);
}
*实现类
@WebService(endpointInterface="com.wms.ws.Common")
public class CommonImpl implements Common {

    private boolean checkeUser(Emp emp, String password) throws Exception {
        if (emp != null && emp.getPassword().equals(Encrypt.encrypt(emp.getEmpNo(), password))) {
            return true;
        } else {
            throw new Exception("用户名或密码错误");
        }
    }
}

2: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:http-conf = "http://cxf.apache.org/transports/http/configuration"    
    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  
        http://cxf.apache.org/transports/http/configuration      
        http://cxf.apache.org/schemas/configuration/http-conf.xsd">         
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  

    <!-- 接口的实现类声明 -->
    <jaxws:endpoint id="Hinwms" implementor="com.wms.ws.CommonImpl"  address="/webservice" />    

</beans>

JAX-RS方式

示例:

  1. java部分
*接口类
@Path(value = "/sample")
public interface  RESTSample {
    
    @POST
    @Path("/bean")
    @Produces({ MediaType.APPLICATION_XML })
    public Hello getBean( Hello id);

}
*实现类
@Path(value = "/sa")
public class RESTSampleSource implements RESTSample {

    @POST
    @Path("/bean")
    @Produces({ MediaType.APPLICATION_XML})
    public Hello getBean(Hello id) {
        id.id+="100";
        return id;
    }

}

2:xml部分

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-4.0.xsd    
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd ">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    
    <bean id="restSample" class="io.raycom.modules.ws.demo.RESTSampleSource"/>
    
    
    <jaxrs:server id="restServiceContainer" address="/sample1">
        <jaxrs:serviceBeans>
            <ref bean="restSample" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
        <jaxrs:languageMappings>
               <entry key="en" value="en-gb"/>  
        </jaxrs:languageMappings>
    </jaxrs:server>   
</beans>
Last modification:October 17th, 2018 at 06:02 pm
If you think my article is useful to you, please feel free to appreciate