spring boot2.x+cxf 搭建webservice服务+postman进行测试:按照步骤操作,没问题的

一: webservice依赖包

<!--webservice-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

二: cxf依赖包

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.1.12</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.12</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.12</version>
</dependency>

三: 创建接口(接口加上@WebService注解)

package com.javaweb.piclesoft.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(
        name = "PsonWebService",     //服务名(直接写接口名)
        targetNamespace = "http://service.piclesoft.javaweb.com"  //命名空间,接口的倒序
)
public interface PsonWebService {

    @WebMethod    //方法注解
    String psonGet(@WebParam(name = "say") String say);   //参数注解
}

四: 创建接口的实现类(实现类加上 @Service@WebService注解)

package com.javaweb.piclesoft.service.impl;

import com.javaweb.piclesoft.service.PsonWebService;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
@WebService(
        name = "PsonWebService",    //和接口服务名一样
        targetNamespace = "http://service.piclesoft.javaweb.com", //和接口的命名空间一样
        endpointInterface = "com.javaweb.piclesoft.service.PsonWebService" //接口的路径
)
public class PsonWebServiceImpl implements PsonWebService {
    @Override
    public String psonGet(String say) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        return say+"时间为: "+simpleDateFormat.format(new Date());
    }
}

五: 创建一个配置类

package com.javaweb.piclesoft.config;

import com.javaweb.piclesoft.service.WebServiceDemoService;
import com.javaweb.piclesoft.service.impl.PsonWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

   
    @Autowired
    private PsonWebService psonWebService ;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }


    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    /**
     * 注册PsonWebService接口到webservice服务
     * @return
     */
    @Bean
    public Endpoint psonEndpoint(){
        EndpointImpl endpoint =new EndpointImpl(springBus(),psonWebService );
        endpoint.publish("/webservice");
        return endpoint;
    }

}

六: 启动项目,谷歌浏览器访问地址http://localhsot:自己的访问端口号/webservice    // 端口号后面的webservice是配置类第一个设置的: (new CXFServlet(),"/webservice/*")

七: postman测试:测试地址为http://localhost:(自己的端口号)/webservice/webservice     1. 端口号后面的webservice是配置类里面:  (new CXFServlet(),"/webservice/*");   2. 第二个webservice是配置类里面:   endpoint.publish("/webservice");   3. postamn请求方法改为: post;  4. Headers添加一个参数:  key = Content-Type   value = text/xml;charset=UTF-8;   5. 在Body 先选择 raw 在选择 xml 格式  复制下面的格式到xml里面;

<?xml version="1.0" encoding="UTF-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://service.piclesoft.javaweb.com">

   <soapenv:Body>

      <test:hello>

        <name><![CDATA[武汉]]> </name>

      </test:hello>

   </soapenv:Body>

</soapenv:Envelope>

注意:

1.  xmlns:test= "填写接口上设置的命名空间".   2.  <test:hello>   hello是接口中的方法名.   3. <name>   name是方法的参数.    4.  <![CDATA[武汉]]>  武汉是访问传入的参数值.

八: 点击Send就有结果啦   结果如下:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

    <soap:Body>

        <ns2:psonGetResponse xmlns:ns2="http://service.piclesoft.javaweb.com1">

            <return>武汉 时间为: 2022-01-21:16:11:42</return>

        </ns2:psonGetResponse>

    </soap:Body>

</soap:Envelope>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值