精华内容
下载资源
问答
  • SpringBoot整合使用WebService
    千次阅读
    2019-01-30 17:40:14

    我使用的SpringBoot的版本为 2.0.3.RELEASE

    首先pom.xml文件中加入WebService的相关依赖:

    <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
          <version>3.2.5</version>
    </dependency>

    WebService接口:

    @javax.jws.WebService(serviceName = "WSServer", // 服务名称
            targetNamespace = "http://service.webservice.com/"// 一般是接口的包名倒序
    )
    public interface WebService {
        @WebMethod
        @WebResult(name = "String", targetNamespace = "")
        public String testWS(@WebParam(name = "msg") String msg);
    
    }

    实现类:

    @javax.jws.WebService(serviceName = "WSServer", // 与接口中指定的name一致
            targetNamespace = "http://service.webservice.com/"// 与接口中的命名空间一致,一般是接口的包名倒序
    )
    @Component
    public class WebServiceImpl implements WebService {
    
           @Override
           public String testWS(String msg){
                 return msg;
           }
    
    }

    发布webservice服务

    import com.webservice.service.WebService;
    import com.webservice.service.WebServiceImpl;
    import org.apache.cxf.Bus;
    import org.apache.cxf.bus.spring.SpringBus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.apache.cxf.transport.servlet.CXFServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.xml.ws.Endpoint;
    
    @Configuration
    public class CxfConfig {
    
        @SuppressWarnings("all")
        @Bean(name = "cxfServlet")
        public ServletRegistrationBean cxfServlet() {
            //创建服务并指定服务名称
            return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
        }
    
    
    
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
    
            return new SpringBus();
        }
    
    
        @Bean
        public WebService webService(){
    
            return new WebServiceImpl();
        }
    
        /**
         * 注册WebServiceDemoService接口到webservice服务
         * @return
         */
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(springBus(),webService());
            endpoint.publish("/webServiceImpl");
            return endpoint;
        }
    
    }
    

    启动服务,在浏览器输入服务ip:port/webservice   即可看到服务的WSDL地址等相关信息。

    接下来使用CXF动态调用我们创建的服务:

            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://192.168.4.100:8080/webservice/webServiceImpl?wsdl");
            // 需要密码的情况需要加上用户名和密码
             //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
            Object[] objects = new Object[0];
            try {
                // invoke("方法名",参数1,参数2,参数3....);
                objects = client.invoke("testWS", "测试WS是否调用成功");
                System.out.println("返回数据======================:" + objects[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }

     调用成功控制台会打印出:

    返回数据======================:测试WS是否调用成功

     

     

     

     

    更多相关内容
  • 1.导入xfire的依赖包xfire-all,会自动导入相关依赖包,其中spring可能会与项目本身的spring冲突,需要将其排除依赖 <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>...

    1.导入xfire的依赖包xfire-all,会自动导入相关依赖包,其中spring可能会与项目本身的spring冲突,需要将其排除依赖

           <dependency>
                <groupId>org.codehaus.xfire</groupId>
                <artifactId>xfire-all</artifactId>
                <version>1.2.6</version>
                <!--排除冲突的依赖包-->
                <exclusions>
                    <exclusion>
                        <groupId>javax.activation</groupId>
                        <artifactId>activation</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    

    2.注入XFireSpringServlet

    @Bean
        public ServletRegistrationBean registrationBean() {
            System.out.println("servletRegistrationBean----------");
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
            servletRegistrationBean.setServlet(new XFireSpringServlet());
            servletRegistrationBean.addUrlMappings("/webservice/*");
            return servletRegistrationBean;
        }
    

    相当于web.xml中的:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	<servlet>
    		<servlet-name>xfireServlet</servlet-name>
    		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>xfireServlet</servlet-name>
    		<url-pattern>/webservice/*</url-pattern>
    	</servlet-mapping>
    </web-app>
    

    3.创建一个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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    	//扫描被@webService的包
        <context:component-scan base-package="com.itender.ms.webService.impl" />
        <!-- XFire start -->
        <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
        <!--<import resource="xfire.xml" />-->
        <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
        <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
            <property name="xfire" ref="xfire" />
            <property name="webAnnotations" ref="webAnnotations" />
        </bean>
    </beans>
    

    4.使用@ImportResource注入xml

    @ImportResource(locations = {"classpath:config/applicationContext.xml"})
    @Component
    public class XfireConfig  {
    
    }
    

    5.创建@WebService接口及实现类

    //webService接口
    @WebService
    public interface IOrgCheckWebService {
    
    }
    //实现类
    //继承SpringBootBeanAutowiringSupport 可以让@Autowired注入成功,我重写了WebApplicationContextLocator的onStartup方法
    @WebService(serviceName = "orgCheckWebService",name = "orgCheckWebService",
            targetNamespace = "http://impl.webService.ms.itender.com")
    @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
    @Service
    public class OrgCheckWebServiceImpl extends SpringBootBeanAutowiringSupport implements IOrgCheckWebService {
    
    }
    
    

    这样就完成了,但是在启动的时候会报错,原因是Spring4.0和xfire1.2.6有版本冲突。修改
    第一个错误:Attribute “singleton” must be declared for element type “bean”.
    因为:xfire定义bean的时候,用了 singleton 属性,Spring4.0取消了singletion
    解决方案:找到xfire-spring-1.2.6.jar中的org/codehaus/xfire/spring的xfire.xml和和xfireXmlBeans.xml,将里边的singleton属性删除
    第二个错误:cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor’
    解决方案:修改xfire-spring-1.2.6.jar中的org/codehaus/xfire/spring/customEditors.xml文件,将< map>< /map>中的内容替换成下面的内容:

    <entry key="org.codehaus.xfire.service.ServiceFactory" value="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"></entry>
    
    

    即将源文件的

    	<map>
            <entry key="org.codehaus.xfire.service.ServiceFactory">
              <bean class="org.codehaus.xfire.spring.editors.ServiceFactoryEditor">
                <property name="transportManager" ref="xfire.transportManager" />
              </bean>
            </entry>
        </map>
    

    替换成

    	<map>
    		<entry key="org.codehaus.xfire.service.ServiceFactory" value="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"></entry>
        </map>
    

    这样,在启动项目就可以成功了,如下图:
    在这里插入图片描述
    注意:修改xml文件的时候可以使用360解压直接打开,然后修改内容,我之前傻乎乎的直接解压出来后修改,之后再重新打成了jar包。。。

    展开全文
  • 文章目录一、集成XFIRE1. 导入依赖2. 注入XFireSpringServlet3. 创建一个xml文件4. 使用@ImportResource注入xml5. 创建@WebService接口6. 创建实现类8. 添加配置类9. 工具类二、发布服务2.1. 运行项目2.2. 异常解决...

    在这里插入图片描述

    一、集成XFIRE
    1. 版本选型
    阿健/框架版本
    spring-boot2.5.4
    xfire-all1.2.6
    2. 导入依赖

    导入xfire的依赖包xfire-all,会自动导入相关依赖包,其中spring可能会与项目本身的spring冲突,需要将其排除依赖

    	  <!--xfire start -->
            <dependency>
                <groupId>org.codehaus.xfire</groupId>
                <artifactId>xfire-all</artifactId>
                <version>1.2.6</version>
                <!--排除冲突的依赖包-->
                <exclusions>
                    <exclusion>
                        <groupId>javax.activation</groupId>
                        <artifactId>activation</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>commons-logging</artifactId>
                        <groupId>commons-logging</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>stax-api</artifactId>
                        <groupId>stax</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--axis end -->
    
    3. 注入XFireSpringServlet

    注入XFireSpringServlet
    创建XfireBootServlet类

    package com.gblfy.ws.servlet;
    
    import org.codehaus.xfire.spring.XFireSpringServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author gblfy
     * @date 2021-09-17
     */
    @Configuration
    public class XfireBootServlet {
    
        @Bean
        public ServletRegistrationBean registrationBean() {
            System.out.println("servletRegistrationBean----------");
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
            servletRegistrationBean.setServlet(new XFireSpringServlet());
            servletRegistrationBean.addUrlMappings("/webservice/*");
            return servletRegistrationBean;
        }
    }
    

    相当于web.xml中的:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	<servlet>
    		<servlet-name>xfireServlet</servlet-name>
    		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>xfireServlet</servlet-name>
    		<url-pattern>/webservice/*</url-pattern>
    	</servlet-mapping>
    </web-app>
    
    
    4. 创建一个xml文件

    在resources下创建cofnig文件夹,并在config文件夹下面创建boot-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"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <!--扫描被@webService的包-->
        <context:component-scan base-package="com.gblfy.ws.service.impl"/>
    
        <!-- XFire start -->
        <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
    
        <!--<import resource="xfire.xml" />-->
        <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>
        <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
            <property name="xfire" ref="xfire"/>
            <property name="webAnnotations" ref="webAnnotations"/>
        </bean>
    </beans>
    
    5. 使用@ImportResource注入xml
    package com.gblfy.ws.config;
    
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.stereotype.Component;
    
    /**
     * 引入boot-xfire.xml配置文件
     *
     * @author gblfy
     * @date 2021-09-17
     */
    @ImportResource(locations = {"classpath:config/boot-xfire.xml"})
    @Component
    public class XfireConfig {
    }
    
    6. 创建@WebService接口
    package com.gblfy.ws.service;
    
    import javax.jws.WebService;
    
    /**
     * Xfire接口
     *
     * @author gblfy
     * @date 2021-09-17
     */
    @WebService
    public interface IXfireService {
        public String sayHello(String info);
        public String sayHello2(String info,String info2);
    }
    
    
    6. 创建实现类
    package com.gblfy.ws.service.impl;
    
    import com.gblfy.ws.service.IXfireService;
    import com.gblfy.ws.utils.SpringBootBeanAutowiringSupport;
    import org.springframework.stereotype.Service;
    
    import javax.jws.WebService;
    import javax.xml.ws.BindingType;
    import javax.xml.ws.soap.SOAPBinding;
    /**
     * Xfire接口实现类
     *
     * @author gblfy
     * @date 2021-09-17
     */
    
    /**
     * serviceName:?wsdl前缀
     * targetNamespace:命名空间
     * name:无实际意义
     */
    @WebService(serviceName = "xfireServiceShell", name = "xfireService",
            targetNamespace = "http://impl.service.ws.gblfy.com")
    @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
    @Service
    //继承SpringBootBeanAutowiringSupport 可以让@Autowired注入成功,我重写了WebApplicationContextLocator的onStartup方法
    public class XfireServiceImpl extends SpringBootBeanAutowiringSupport implements IXfireService {
    
        /**
         * @param info
         * @return
         */
        @Override
        public String sayHello(String info) {
            return "sayHello:" + info;
        }
    
        @Override
        public String sayHello2(String info, String info2) {
            return info + info2;
        }
    }
    
    
    7. 添加配置类

    WebApplicationContextLocator

    package com.gblfy.ws.config;
    
    import org.springframework.boot.web.servlet.ServletContextInitializer;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    
    /**
     * 手动获取bean
     *
     * @author gblfy
     * @date 2021-09-17
     */
    @Configuration
    public class WebApplicationContextLocator  implements ServletContextInitializer {
    
        private static WebApplicationContext webApplicationContext;
    
        public static WebApplicationContext getCurrentWebApplicationContext() {
            return webApplicationContext;
        }
    
        /**
         * 在启动时将servletContext 获取出来,后面再读取二次使用。
         * @param servletContext
         * @throws ServletException
         */
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        }
    }
    
    8. 工具类

    SpringBootBeanAutowiringSupport

    package com.gblfy.ws.utils;
    
    import com.gblfy.ws.config.WebApplicationContextLocator;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
    import org.springframework.util.Assert;
    import org.springframework.util.ClassUtils;
    import org.springframework.web.context.WebApplicationContext;
    
    /**
     * 手动获取bean
     *
     * @author gblfy
     * @date 2021-09-17
     */
    public abstract class SpringBootBeanAutowiringSupport {
    
        private static final Logger logger = LoggerFactory.getLogger(SpringBootBeanAutowiringSupport.class);
    
    
        /**
         * This constructor performs injection on this instance,
         * based on the current web application context.
         * <p>Intended for use as a base class.
         *
         * @see #processInjectionBasedOnCurrentContext
         */
        public SpringBootBeanAutowiringSupport() {
            System.out.println("SpringBootBeanAutowiringSupport.SpringBootBeanAutowiringSupport");
            processInjectionBasedOnCurrentContext(this);
        }
    
    
        /**
         * Process {@code @Autowired} injection for the given target object,
         * based on the current web application context.
         * <p>Intended for use as a delegate.
         *
         * @param target the target object to process
         * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
         */
        public static void processInjectionBasedOnCurrentContext(Object target) {
            Assert.notNull(target, "Target object must not be null");
            WebApplicationContext cc = WebApplicationContextLocator.getCurrentWebApplicationContext();
            if (cc != null) {
                AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
                bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
                bpp.processInjection(target);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Current WebApplicationContext is not available for processing of " +
                            ClassUtils.getShortName(target.getClass()) + ": " +
                            "Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
                }
            }
        }
    }
    
    

    这样就完成了!!!

    二、XFIRE 发布服务
    2.1. 运行项目

    在这里插入图片描述

    2.2. 异常解决

    但是在启动的时候遇到Attribute “singleton” must be declared for element type “bean”.
    在这里插入图片描述
    移步跳转,即可解决:
    Attribute “singleton” must be declared for element type “bean”.

    移步跳转,即可解决:
    遇到cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor’
    cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor

    2.3. 测试验证

    http://localhost:8080//webservice/xfireServiceShell?wsdl
    在这里插入图片描述

    三、XFIRE客户端
    package com.gblfy.ws.client;
    
    import org.codehaus.xfire.client.Client;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import java.net.URL;
    
    @Component
    public class XFireClient {
        private final static Logger log = LoggerFactory.getLogger(XFireClient.class);
    
        public static void main(String[] args) throws Exception {
    
            String xfireUrl = "http://localhost:8080/xfire/xfireServiceShell?wsdl";
            String namespaceURI = "http://impl.service.ws.gblfy.com";
            //单个参数
            String method = "sayHello";
    
            //多参
            // String method = "sayHello2";
            String reqXml = "1";
            String reqXml2 = "2";
    
            //调用服务
            XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml);
            // XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml, reqXml2);
        }
    
    
        /**
         * 单参调用工具类
         *
         * @param xfireUrl url地址
         * @param method   调用方法名
         * @param reqXml   发送报文体
         * @return res 返回结果
         * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
         */
        public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml) throws Exception {
            // 创建服务
            Client client = new Client(new URL(xfireUrl));
            // 设置调用的方法和方法的命名空间
            client.setProperty(namespaceURI, method);
            // 通过映射获得结果
            Object[] result = new Object[0];
            try {
                result = client.invoke(method, new Object[]{reqXml});
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            String xml = (String) result[0];
            log.info("响应报文 : {}", xml);
            return xml;
        }
    
        /**
         * 多参调用工具类(Object类型)
         *
         * @param xfireUrl url地址
         * @param method   调用方法名
         * @param reqXml   发送报文体
         * @return res 返回结果
         * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
         */
        public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml, String reqXml2) throws Exception {
    
            // 创建服务
            Client client = new Client(new URL(xfireUrl));
            // 设置调用的方法和方法的命名空间
            client.setProperty(namespaceURI, method);
            // 通过映射获得结果
            Object[] result = new Object[0];
            try {
                result = client.invoke(method, new Object[]{reqXml, reqXml2});
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            String xml = (String) result[0];
            log.info("响应报文 : {}", xml);
            return xml;
        }
    }
    
    
    开源源码.

    https://gitee.com/gb_90/unified-access-center

    展开全文
  • Webservice集成Xfire

    2019-04-22 01:28:13
    NULL 博文链接:https://zhangzhi199129.iteye.com/blog/1953901
  • XFire 和spring完整集成实例

    热门讨论 2012-03-06 14:26:04
    import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; ...
  • 解决了Spring2.0和XFire1.2.6整合出错的问题 如果你觉得我骗分请不要下,谢谢!
  • spring3.0整合Xfire1.2.6 开发webservice需要的jar包
  • springboot整合servlet两种方式

    千次阅读 2018-09-19 14:49:02
    springboot整合servlet方式一: 项目结构: pom.xml: &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project xmlns="http://maven.apache.org/POM/4.0.0" ...

    springboot与servlet整合的两种方式:

    1. 方式一:

    项目结构:

    pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.lucifer.springboot</groupId>
        <artifactId>springboot-servlet</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springboot-servlet</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

     SpringbootServletApplication : springboot项目的启动类

    package com.lucifer.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    
    /**
     * @ServletComponentScan
     * 在springboot启动时会扫描@WebServlet,并将该类实例化
     */
    @SpringBootApplication
    @ServletComponentScan
    public class SpringbootServletApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootServletApplication.class, args);
        }
    }
    

    FirstServet:

    启动项目,浏览器输入localhost:8080/first:

    ps: 整合过程中新手很容易遇坑的地方:原本我的项目结构是这样的,如下图:

    这时候启动项目,控制台无报错,浏览器输入地址,404,原因就是因为com.lucifer.springboot.servlet这个包没有扫描到。

     那百度了,按照百度查到的资料,加了扫描包的注解,没用,效果依然凉凉。

    因此这里就把启动类放在com.lucifer.springboot包下,FirstServet就包在子包com.lucifer.springboot.servlet下,启动类的扫描默认是扫描与该启动类同包以及其子包下的类。

    2.方式二:

    项目结构:

    SecondServlet :

    package com.lucifer.springboot.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * @author: Lucifer
     * @create: 2018-09-19 14:39
     * @description:
     **/
    public class SecondServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().append("springboot整合servlet成功");
        }
    }
    

    SpringbootServletApplication:

    package com.lucifer.springboot;
    
    import com.lucifer.springboot.servlet.SecondServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    
    
    @SpringBootApplication
    public class SpringbootServletApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootServletApplication.class, args);
        }
    
        @Bean
        public ServletRegistrationBean getservletRegistrationBean(){
            ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
            bean.addUrlMappings("/second");
            return bean;
        }
    
    }
    

     

    展开全文
  • 二、目前比较主流的web服务实现方法 Apache Axis1、Apache Axis2、Codehaus XFire、Apache CXF、Apache Wink、Jboss RESTEasy、sun JAX-WS(最简单、方便)、阿里巴巴 Dubbo等 Rest:简单易用,效率高 Soap:成熟度...
  • 工作中有时候会遇到要写一些webservice接口情况,虽然这个技术目前已经不流行了,但一些传统行业还是在广泛使用的,我主要使用SpringBoot结合CXF框架,可以快速搭建起这种服务,项目是用Maven构建的,先附上依赖的...
  • Spring中整合Xfire发布webservice服务

    千次阅读 2016-04-20 16:49:18
    一、web.xml中配置xfire的servlet交由spring管理 contextConfigLocation /WEB-INF/applicationContext-*.xml org.springframework.web.context.ContextLoaderListener XFireServlet ...

空空如也

空空如也

1 2 3 4
收藏数 75
精华内容 30
关键字:

springboot整合xfire

spring 订阅