目录结构:
pom文件:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
代码:
接口:TestService
package com.viewhigh.gov.acctWebService.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface TestService {
@WebMethod
String queryList(@WebParam(name = "userId") String userId, @WebParam(name = "agentNum") Integer agentNum);
}
实现:TestServiceImpl
package com.viewhigh.gov.acctWebService.service.impl;
import com.viewhigh.gov.acctWebService.service.TestService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@WebService(targetNamespace = "http://webservice.tibom.thit.com/",
endpointInterface = "com.viewhigh.gov.acctWebService.service.TestService")
@Component
public class TestServiceImpl implements TestService {
@Override
public String queryList(String userId, Integer agentNum) {
return "请求成功";
}
}
发布方法:WebServiceConfig
package com.viewhigh.gov.acctWebService.service;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Autowired
private TestService testService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), testService);
endpoint.publish("/testService");
return endpoint;
}
}
遇到问题:
Service endpoint interface does not have a @WebService annotation。
一开始没仔细看这个错误,照着网上的给的方式写,还是一直报这个错。
后来仔细看了看这个错误,原来接口也是要注释成@WebService的。