springBoot如何编写一个webService服务呢?

java-经验王 SpringBoot 发布时间:2022-05-02 09:40:51 阅读数:12837 1
下文笔者讲述使用springBoot编写一个webService的方法分享,如下所示:

pom中加入webservice依赖-开启指定的starter器

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

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<User> 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<User> findByName(String name) {
        List<User> 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的发布
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202205/3233.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者