公司让用springboot 写一个webservice接口,从网上一步一步来,但是我的就出现了好多无法解决得问题,那种百度都百度不到得:上代码直接:
pom.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
**`接口代码**
package com.casic.Wsdl;
import com.casic.Util.Response;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* 数据同步接口
*/
@WebService(name="DatasyncServce",
targetNamespace = "http://Wsdl.casic.com")//与接口命名空间一致
public interface DatasyncService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public Response synvhronOrg(@WebParam(name = "orgization") String org);
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public Response userinfo(@WebParam(name = "user") String persion);
}
对应实现类
package com.casic.Wsdl.Impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.casic.Bean.Orgization;
import com.casic.Bean.User;
import com.casic.Service.IOrgizationService;
import com.casic.Service.IUserService;
import com.casic.Util.ParserXmlUtil;
import com.casic.Util.Response;
import com.casic.Wsdl.DatasyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.jws.WebService;
/**
*/
@WebService(serviceName = "DatasyncService",
targetNamespace = "http://Wsdl.casic.com",
endpointInterface = "com.casic.Wsdl.DatasyncService")
@Component
public class DatasyncServiceImpl implements DatasyncService {
@Autowired
private IUserService userService;
@Autowired
private IOrgizationService orgizationService;
@Override
public Response synvhronOrg(String org) {
//对应逻辑删除了
return rs;
}
@Override
public Response userinfo(String persion) {
//对应逻辑删除了
return ....
}
}
CXF配置文件
package com.casic.Config;
import com.casic.Wsdl.DatasyncService;
import com.casic.Wsdl.Impl.DatasyncServiceImpl;
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.beans.factory.annotation.Autowired;
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 {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DatasyncService datasyncService() {
return new DatasyncServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), datasyncService());
endpoint.publish("/DatasyncService");
return endpoint;
}
}
用这种方法之后会报错,报错信息如下
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-01-21 11:15:50.379 ERROR 8852 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
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.
之后怎么找都找不到哪里错了,无奈只能换一种方法,将cxfconfig文件修改如下
public class Cxfconfig {
@Autowired
private Bus bus;
@Autowired
private DatasyncService datasyncService;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, datasyncService);
endpoint.publish("/DatasyncService");
return endpoint;
}
}
但是出现这个问题
Description:
Field bus in com.casic.Config.Cxfconfig required a bean of type 'org.apache.cxf.Bus' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.apache.cxf.Bus' in your configuration.
然后之前游览博客说是我jar包用的不对,于是我将这3个jar
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.4</version>
</dependency>
换成了
<!--<dependency>-->
<!--<groupId>org.apache.cxf</groupId>-->
<!--<artifactId>cxf-spring-boot-starter-jaxws</artifactId>-->
<!--<version>3.1.11</version>-->
<!--</dependency>-->
结果项目启动出现报错
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException:Unable to start embedded Tomcat
解决了3天都没有解决,所以无奈只能求助博客得各位大神…