转自:
springBoot如何编写一个webService服务呢?
下文笔者讲述使用springBoot编写一个webService的方法分享,如下所示:
pom中加入webservice依赖-开启指定的starter器
org.apache.cxf
cxf-spring-boot-starter-jaxws
3.2.5
java后台对外暴露webService
接口定义
import com.comjava265.model.AchieveExpress;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.List;
@WebService(name = "MaoMaoService", // 对外的服务名称
targetNamespace = "http://service.web.java265.com") //命名空间,一般是接口的包名倒序
public interface MaoMaoService {
@WebMethod
@WebResult(name = "String",targetNamespace = "")
List findByName(@WebParam(name = "name") String name);
}
--具体的实现类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.List;
@WebService(serviceName = "MaoMaoService",//与前面接口一致
targetNamespace = "http://service.web.java265.com", //与前面接口一致
endpointInterface = "com.java265.web.service.MaoMaoService") //接口地址
@Component
public class MaoMaoServiceImpl implements MaoMaoService {
@Autowired
UserService userService;
@Override
public List findByName(String name) {
List res= userService.findByName(name);
return res;
}
}
------服务端发布相关的接口(可发布多个)
import org.apache.cxf.Bus;
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;
/**
* webservice
*/
@Configuration
public class WebConfig {
@Autowired
private Bus bus;
@Autowired
MaoMaoService maoMaoService;
@Autowired
AchieveExpressService achieveExpressService;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, maoMaoService);
endpoint.publish("/MaoMaoService");
return endpoint;
}
@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(bus, maoMaoService);
endpoint.publish("/MaoMaoService2");
return endpoint;
}
}
采用以上方式,即可完成webService的发布