国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot實現(xiàn)webservice接口自定義返回值通過postman測試

這篇具有很好參考價值的文章主要介紹了springboot實現(xiàn)webservice接口自定義返回值通過postman測試。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

震驚~~都2023年了竟然還有人用webservice!

1、springboot集成webservice

maven添加依賴

        <!--通過cxf實現(xiàn)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 {

? ? //自定義攔截器 用于處理返回數(shù)據(jù)格式
    @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());
        //發(fā)布一個名為AccountService的webservice服務(wù),可以多個
? ? ? ? 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實現(xiàn)wenservice服務(wù)返回結(jié)果是有一層固定包裝的,類似下圖,return標(biāo)簽里才是結(jié)果,如何完全自定義返回的結(jié)果數(shù)據(jù)呢?就需要上面的攔截器去掉外層的包裝。

springboot實現(xiàn)webservice接口自定義返回值通過postman測試,積累,spring boot,postman,java,Powered by 金山文檔

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();

}

實現(xiàn)類

@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;
    }

}

接下來啟動服務(wù),瀏覽器訪問ip:端口/webservice/AccountService?wsdl

即可看到wsdl信息

springboot實現(xiàn)webservice接口自定義返回值通過postman測試,積累,spring boot,postman,java,Powered by 金山文檔

2、postman測試

Headers添加

key:Content-type

value:text/xml;charset=utf-8

springboot實現(xiàn)webservice接口自定義返回值通過postman測試,積累,spring boot,postman,java,Powered by 金山文檔

然后Body選擇raw,XML

springboot實現(xiàn)webservice接口自定義返回值通過postman測試,積累,spring boot,postman,java,Powered by 金山文檔
<?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>

如果使用了上面的自定義攔截器,則調(diào)用的返回結(jié)果為:文章來源地址http://www.zghlxwxcb.cn/news/detail-597046.html

springboot實現(xiàn)webservice接口自定義返回值通過postman測試,積累,spring boot,postman,java,Powered by 金山文檔

到了這里,關(guān)于springboot實現(xiàn)webservice接口自定義返回值通過postman測試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 使用postman 調(diào)用 Webservice 接口

    使用postman 調(diào)用 Webservice 接口

    使用postman 調(diào)用 Webservice 接口,postman,webService,接口,cxf

    2024年02月08日
    瀏覽(30)
  • SpringBoot調(diào)用第三方WebService接口的兩種實現(xiàn)方式

    SpringBoot調(diào)用第三方WebService接口的兩種實現(xiàn)方式

    WebService接口的發(fā)布通常一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,該文檔包含了請求的參數(shù)信息,返回的結(jié)果信息,我們需要根據(jù)WSDL文檔的信息來編寫相關(guān)的代碼進行調(diào)用WebService接口。接下來我將采用常見的兩種方式調(diào)用WebService接口。 目前我需要

    2024年02月12日
    瀏覽(19)
  • Postman進行Soap webservice接口測試

    Postman進行Soap webservice接口測試

    許多人認(rèn)為Postman是高級REST客戶端,Postman是處理通過HTTP發(fā)送的請求的工具。其實Postman也可以測試與協(xié)議無關(guān)的SOAP webservice api接口。 要使用Postman發(fā)出SOAP請求,請執(zhí)行以下操作: 1、提供SOAP端點作為URL,可以使用SOAP的WSDL的路徑作為URL。 2、將請求方法設(shè)置為POST。 3、Body中選

    2024年01月20日
    瀏覽(29)
  • C# 調(diào)用SAP WebService接口(SoapUI Postman)

    C# 調(diào)用SAP WebService接口(SoapUI Postman)

    SAP wsdl地址發(fā)放在瀏覽器中是需要輸入用戶名密碼進行認(rèn)證的 將wsdl地址放到SoapUI進行解析 輸入用戶名密碼 左邊就是請求的XML格式,右邊是接口返回內(nèi)容,點擊運行就可以看到結(jié)果 Request1的地址就是我們需要調(diào)用的地址(這里提供的域名和wsdl 的域名不一致,需要換成wsdl的域

    2024年02月09日
    瀏覽(31)
  • Java調(diào)用WebService接口,SOAP協(xié)議HTTP請求返回XML對象

    Java調(diào)用Web service接口SOAP協(xié)議HTTP請求,解析返回的XML字符串: 1. 使用Java的HTTP庫發(fā)送SOAP請求,并接收返回的響應(yīng)。 可以使用Java的HttpURLConnection、Apache HttpClient等庫。 2. 將返回的響應(yīng)轉(zhuǎn)換為字符串。 3. 解析XML字符串 ,可以使用Java的DOM解析器或者其他第三方庫,如JDOM、DOM4J等。

    2024年01月19日
    瀏覽(28)
  • C# webservice 接收json數(shù)據(jù) 接口返回 遠程服務(wù)器返回錯誤: (500) 內(nèi)部服務(wù)器錯誤

    C# webservice 接收json數(shù)據(jù) 接口返回 遠程服務(wù)器返回錯誤: (500) 內(nèi)部服務(wù)器錯誤

    C# post 調(diào)用webservice 服務(wù)端接口,會返回上面那個錯誤,8成是發(fā)送的數(shù)據(jù)和接口不符合造成的。有2種情況 第一種情況如下:如果類型是默認(rèn)request.ContentType = \\\"application/x-www-form-urlencoded\\\";這個類型 那么你發(fā)送數(shù)據(jù)和被調(diào)用接口參數(shù)名如果不對,則會報下圖這個錯 我發(fā)送的參數(shù)名

    2024年02月13日
    瀏覽(26)
  • 如何通過postman實現(xiàn)接口請求

    目錄 1.創(chuàng)建一個集合 2.創(chuàng)建一個文件夾 3.在文件夾中創(chuàng)建一個基本請求 ? ? ?*設(shè)置請求的方式:get、post、delete、put ? ? ?*設(shè)置請求的url地址 ?????*設(shè)置請求的請求體body或請求參數(shù)params: ? ? ? *設(shè)置請求頭: 4.設(shè)置斷言 5.點擊send發(fā)送 6.配置環(huán)境 7.獲取請求體中的數(shù)據(jù)并設(shè)置為

    2024年02月04日
    瀏覽(24)
  • 根據(jù)WebService接口地址獲取接口定義文件(wsdl文件)

    根據(jù)WebService接口地址獲取接口定義文件(wsdl文件)

    目錄 方法一:借助SoapUI生成 方法二:借助Visual Studio工具生成 根據(jù)webservice接口地址想生成wsdl文件,可以借助SoapUI生成 選中,右鍵,導(dǎo)出 以管理員身份運行命令行窗口,進入VS插件目錄,使用svcutil.exe工具自動生成wsdl文件。 命令:cd C:Program Files (x86)Microsoft SDKsWindowsv7.0A

    2023年04月12日
    瀏覽(40)
  • SAP PO 接口配置1:連通WebService-通過PO調(diào)用第三方接口

    SAP PO 接口配置1:連通WebService-通過PO調(diào)用第三方接口

    SAP 通過 PO 中間件進行接口調(diào)用,調(diào)用外部接口。 外部接口可以用任意方式生成,常見的REST類型接口即可,關(guān)于如何使用python生成接口,其他章節(jié)另述。 本教程的前置條件,PO中已配置Business Systems,并與SAP環(huán)境連通。 這里以常見的post接口做示例,如有其他類型接口,需要每

    2024年02月05日
    瀏覽(93)
  • java prometheus 自定義exporter開發(fā),以及實現(xiàn)多個接口返回metrics

    ??exporter的作用是采集需要監(jiān)控的數(shù)據(jù),并將采集到的數(shù)據(jù)轉(zhuǎn)換成prometheus所需要的數(shù)據(jù)格式,將這些轉(zhuǎn)換后的數(shù)據(jù)返回,供給prometheus 使用。 java 編寫自定義exporter所需要的pom.xml: exporter的四類指標(biāo)說明 數(shù)據(jù)類型 解釋 Counter Counter類型代表一種樣本數(shù)據(jù)單調(diào)遞增的指標(biāo),即

    2023年04月08日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包