spring boot (使用@Configuration发布)发布 web service(使用CXF,SOAP)接口,以及遇到的一些问题

一,创建springboot项目
二,导入依赖

	
         org.apache.cxf
          cxf-rt-frontend-jaxws
          3.1.12
      
      
          org.apache.cxf
          cxf-rt-transports-http
          3.1.12
      
      

      
          org.apache.cxf
          cxf-rt-transports-http-jetty
          3.1.6
      

三,创建实体类,(这个根据自己需要,可以不创建)

public class User implements Serializable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

四,创建接口,以及实现类

@WebService
@WebServiceProvider
public interface UserService {
    @WebMethod
    public User findUserById( String id) ;
    @WebMethod
    public String addUser(User user);

}
================================================================

public class UserServiceImpl implements UserService {

    public User findUserById(String id) {
        User user = new User();
        user.setAge(12);
        user.setName("haha");
        return user;
    }
    public String addUser(User user) {
        return "ok";
    }
}

五,发布服务
第一种方式:直接在启动类中使用Endpoint,不需编写配置类,这种方式可以自定义webservice的端口,但不要和服务器的端口冲突了。(注意上面的pom依赖)

@SpringBootApplication
public class WebserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebserviceApplication.class, args);
        Endpoint.publish("http://localhost:9999/webService/userService/", new UserServiceImpl());
    }

}

第二种方式:使用配置类,不用在启动类中加Endpoint.publish,这种接口的端口号和服务器端口号是一致的。

@Configuration
public class CxfConfig {
    @Bean
    public ServletRegistrationBean disServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");
        return servletRegistrationBean;
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), new UserServiceImpl());
        endpoint.publish("/userService");
        return endpoint;
    }
}


注意:网上很多教程的配置类中,第一个@Bean的方法名都是

public ServletRegistrationBean dispatcherServlet(){....}

这样会报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
The following candidates were found but could not be injected:
	- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet

Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.

把那个方法名改一下,就行了!

然后就可以启动服务了,
访问localhost:port/webService/userService?wsdl
就能成功显示接口信息

你可能感兴趣的:(webService,SOAP,cxf)