0


springboot实现webservice接口自定义返回值通过postman测试

震惊~~都2023年了竟然还有人用webservice!

1、springboot集成webservice

maven添加依赖

        <!--通过cxf实现webservice-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--解析xml-->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
        </dependency>

添加配置文件

@Configuration
public class CxfConfig {

    //自定义拦截器 用于处理返回数据格式
    @Autowired
    private CxfOutterceptor cxfOutterceptor;

    @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 AccountServiceImpl());
        //发布一个名为AccountService的webservice服务,可以多个
        endpoint.publish("/AccountService");
        endpoint.getOutInterceptors().add(cxfOutterceptor);
        return endpoint;
    }

}

自定义拦截器

@Component
public class CxfOutterceptor extends AbstractPhaseInterceptor<Message> {

    public CxfOutterceptor() {
        super(Phase.PRE_STREAM);
    }

    public CxfOutterceptor(String phase) {
        super(phase);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        try {
            OutputStream os = message.getContent(OutputStream.class);
            CachedStream cs = new CachedStream();
            message.setContent(OutputStream.class, cs);
            message.getInterceptorChain().doIntercept(message);
            CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
            InputStream in = csnew.getInputStream();
            String xml = IOUtils.toString(in);

            StringBuffer stringBuffer = new StringBuffer();

            //去掉外层包装
            Document document = DocumentHelper.parseText(xml);
            Element rootElement = document.getRootElement();
            List<Element> elements1 = rootElement.elements();
            if(elements1!=null && elements1.size()>0){
                Element element = elements1.get(0);
                List<Element> elements = element.elements();
                if(elements!=null && elements.size()>0){
                    Element aReturn = elements.get(0).element("return");
                    String name = aReturn.getName();
                    String adata = aReturn.getData().toString();

                    stringBuffer = new StringBuffer(adata);

                }
            }

            //这里对xml做处理,处理完后同理,写回流中
            IOUtils.copy(new ByteArrayInputStream(stringBuffer.toString().getBytes()), os);

            cs.close();
            os.flush();

            message.setContent(OutputStream.class, os);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class CachedStream extends CachedOutputStream {

        public CachedStream() {

            super();

        }

        protected void doFlush() {

            //currentStream.flush();

        }

        protected void doClose(){

        }

        protected void onWrite(){

        }

    }

}

通过cxf实现wenservice服务返回结果是有一层固定包装的,类似下图,return标签里才是结果,如何完全自定义返回的结果数据呢?就需要上面的拦截器去掉外层的包装。

service类

@WebService(name = "AccountService",
        targetNamespace = "http://server.webservice.kaka.com"
)
public interface AccountService {

    @WebMethod
    public String sendMessage(@WebParam(name = "username",targetNamespace="http://server.webservice.kaka.com") String username);

    @WebMethod
    public boolean getFlag(@WebParam(name = "username",targetNamespace="http://server.webservice.kaka.com") String username);

    @WebMethod
    public String queryUsers();

}

实现类

@WebService(serviceName = "AccountService",
        //名命空间可任意写 要跟service里的保持一致
        targetNamespace = "http://server.webservice.kaka.com",
        //接口类的全路径
        endpointInterface = "com.kaka.webService.AccountService"
)
@Component
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Override
    public String sendMessage(String username) {
        return "=====Hello! " + username + "=====";
    }

    @Override
    public boolean getFlag(String username) {
        return false;
    }

}

接下来启动服务,浏览器访问ip:端口/webservice/AccountService?wsdl

即可看到wsdl信息

2、postman测试

Headers添加

key:Content-type

value:text/xml;charset=utf-8

然后Body选择raw,XML

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <sendMessage xmlns="http://server.kaka.com">
            <username>测试啊</username>
    </sendMessage>
    </soap:Body>
</soap:Envelope>

如果使用了上面的自定义拦截器,则调用的返回结果为:


本文转载自: https://blog.csdn.net/qq_39731741/article/details/129402734
版权归原作者 角谷 所有, 如有侵权,请联系我们删除。

“springboot实现webservice接口自定义返回值通过postman测试”的评论:

还没有评论