WebService接口发布的2种方式

本文为自己开发遇到的问题做一个记录,用的是springboot,发布的时候2种方式同时发布wsdl的接口,调用方式有些不同。

1. xfire
使用jdk自带的注解的方式创建webservice,JAX-WS jdk创建

xfire方式需要一个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


        <!-- 发布webservice -->


    <bean id="xfire.serviceRegistry"
          class="org.codehaus.xfire.service.DefaultServiceRegistry"/>

    <bean id="xfire.transportManager"
          class="org.codehaus.xfire.transport.DefaultTransportManager"
          init-method="initialize" destroy-method="dispose">
    </bean>

    <bean id="xfire" class="org.codehaus.xfire.DefaultXFire">
        <constructor-arg index="0">
            <ref bean="xfire.serviceRegistry" />
        </constructor-arg>
        <constructor-arg index="1">
            <ref bean="xfire.transportManager" />
        </constructor-arg>
    </bean>

    <bean id="xfire.typeMappingRegistry"
          class="org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry"
          init-method="createDefaultMappings" >
    </bean>

    <bean id="xfire.aegisBindingProvider"
          class="org.codehaus.xfire.aegis.AegisBindingProvider">
        <constructor-arg index="0">
            <ref bean="xfire.typeMappingRegistry" />
        </constructor-arg>
    </bean>

    <bean id="xfire.serviceFactory"
          class="org.codehaus.xfire.service.binding.ObjectServiceFactory">
        <constructor-arg index="0">
            <ref bean="xfire.transportManager" />
        </constructor-arg>
        <constructor-arg index="1">
            <ref bean="xfire.aegisBindingProvider" />
        </constructor-arg>
    </bean>

    <bean id="xfire.servletController"
          class="org.codehaus.xfire.transport.http.XFireServletController">
        <constructor-arg>
            <ref bean="xfire" />
        </constructor-arg>
    </bean>

    <bean id="xfire.messageServiceFactory"
          class="org.codehaus.xfire.service.binding.ObjectServiceFactory">
        <constructor-arg index="0" ref="xfire.transportManager" />
        <constructor-arg index="1" ref="xfire.messageBindingProvider" />
        <property name="style" value="message" />
    </bean>

    <bean id="xfire.messageBindingProvider"
          class="org.codehaus.xfire.service.binding.MessageBindingProvider" />
    <!-- START SNIPPET: xfire -->
    <!--     <bean
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="urlMap">
                <map>
                    <entry key="/orderService">
                        <ref bean="orderService" />
                    </entry>
                </map>
            </property>
        </bean> -->

    <!-- 定义一个父类,让所有服务都可共用,而不需要每次都加上XFire的配置 -->
    <bean id="XFireAbstractBean"
          class="org.codehaus.xfire.spring.remoting.XFireExporter"
          abstract="true">
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory" />
        </property>
        <property name="xfire">
            <ref bean="xfire" />
        </property>
    </bean>

</beans>


暴露的接口



import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService( name = "DocService ",// 暴露服务名称
        targetNamespace = "http://webservice.kong.com"// 命名空间,一般是接口的包名倒序
)
public interface DocService {
  @WebMethod
    @WebResult(name = "arg0", targetNamespace = "http://webservice.kong.com")
    public String DocServicePAT(@WebParam(name = "arg0") String arg0);


}

暴露接

口的实现


@WebService(serviceName = "DocServiceImpl ", // 与接口中指定的name一致
        targetNamespace = "http://webservice.kong.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.kong.webservice.DocServiceImpl "// 接口地址
)
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@Service
@Component
@Slf4j
public class DocServiceImpl implements DocService{
 @Override
    public String acceptDocumentTask(String request) {
       }
}
Config配置

@Configuration
@ImportResource(locations = {"classpath:applicationContext.xml"})
public class WsdlConfig {
 
    @Autowired
    private DocService DocService ;

    //xfire发布接口 需要搭配ImportResource(locations = {"classpath:applicationContext.xml"}), 需在发布的Service和Service实现类都加
    /**
       @WebService(serviceName = "DocService ", // 与接口中指定的name一致
               targetNamespace = "http://webservice.kong.com", // 与接口中的命名空间一致,一般是接口的包名倒
               endpointInterface = "com.kong.webservice.DocService"// 接口地址
       )
     */
    // //
    @Bean
    public ServletRegistrationBean<XFireSpringServlet> xfireServlet() {
        ServletRegistrationBean<XFireSpringServlet> registrationBean = new ServletRegistrationBean<>(new XFireSpringServlet(),"/ws/*");
        registrationBean.setName("Xfire");
        return registrationBean;
    }

    @Bean
    public WebAnnotations webAnnotations() {
        return new Jsr181WebAnnotations();
    }

    @Bean
    public Jsr181HandlerMapping jsr181HandlerMapping(@Autowired XFire xFire) {
        Jsr181HandlerMapping jsr181HandlerMapping = new Jsr181HandlerMapping();
        jsr181HandlerMapping.setXfire(xFire);
        jsr181HandlerMapping.setWebAnnotations(webAnnotations());
        jsr181HandlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return jsr181HandlerMapping;
    }
}

2. cxf 

导入cxf的相关jar
Config配置


@Configuration 
public class WsdlConfig {
 
    @Autowired
    private WSService WSService ;
    @Autowired
    private DocService DocService ;


    cxf发布接口  搭配Endpoint
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean(name = "wsBean")
    public ServletRegistrationBean dispatcherServlet() {
        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/ws/*");///发布路径配置
        return wbsServlet;
    }

    @Bean
    public Endpoint endpointPurchase() {
        EndpointImpl endpoint = new EndpointImpl( springBus(), WSService );
        endpoint.publish("/NCCloud_WS_Service");
//        System.out.println("服务发布成功!地址为:http://127.0.0.1:90011/TIMS/ws/WSService ?wsdl");
        return  endpoint;
    }
    @Bean
    public Endpoint endpoint2() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), DocService );
        endpoint.publish("/NC_WS_Service");
//        System.out.println("服务发布成功!地址为:http://127.0.0.1:90011/TIMS/ws/DocService ?wsdl");
        return   endpoint;
    }
    

    cxf发布接口 
}

接口配置


import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * @Author kdc
 **/

@WebService(name = "WSService", // 暴露服务名称
        targetNamespace = "http://WebService.kong.com"// 命名空间,一般是接口的包名倒序
)
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface WSService {
 
    @WebMethod
    @WebResult(name = "arg0", targetNamespace = "http://WebService.kong.com")
    public String acceptDocsk(@WebParam(name = "arg0") String arg0);
}

接口实现类


@Service 
@Component
public class WSServiceImpl implements WSService {

 
    @Override
    public String acceptDocsk(String req) {
        
            return "false"; 
    }


}



想要了解更多移步:WebService 四种发布方式总结_was发布jar项目-CSDN博客

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个开源的框架,用于快速构建基于Java的应用程序。它可以轻松地集成和发布WebService接口。 要发布一个WebService接口,首先需要在Spring Boot项目的pom.xml文件中添加相应的依赖。例如,可以添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 接下来,需要创建一个用于定义WebService接口Java类。这个类应该使用`@WebService`注解进行标记,并且定义所需的接口方法。 例如,可以创建一个名为`HelloWebService`的类,其中定义了一个`sayHello`方法: ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloWebService { @WebMethod public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 接着,在Spring Boot应用程序的入口类中,使用`@Endpoint`注解将此WebService接口发布为服务。同时,需要创建用于启动应用程序的`main`方法,并启动Spring Boot应用程序。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import javax.xml.ws.Endpoint; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public Endpoint endpoint(Bus bus, HelloWebService helloWebService) { EndpointImpl endpoint = new EndpointImpl(bus, helloWebService); endpoint.publish("/hello"); // 指定发布的URL路径 return endpoint; } @Bean public Bus bus() { return new SpringBus(); } } ``` 在上述代码中,`HelloWebService`类作为`endpoint`方法的参数传递给`EndpointImpl`实例,然后调用`publish`方法发布WebService接口。 现在,启动Spring Boot应用程序,并访问`http://localhost:8080/hello`,就可以使用刚才定义的WebService接口了。例如,可以通过SOAP协议使用该接口访问`sayHello`方法。 这就是利用Spring Boot发布WebService接口的基本步骤。通过以上步骤,可以方便地使用Spring Boot构建和发布自己的WebService接口,实现灵活的Web服务开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值