栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一: webservice依赖包


    org.springframework.boot
    spring-boot-starter-web-services

二: cxf依赖包


    org.apache.cxf
    cxf-core
    3.1.12



    org.apache.cxf
    cxf-rt-frontend-jaxws
    3.1.12



    org.apache.cxf
    cxf-rt-transports-http
    3.1.12

三: 创建接口(接口加上@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 ;

    
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice
    @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里面;

   

      

         

      

   

注意:

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

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

    

        

            武汉 时间为: 2022-01-21:16:11:42

        

    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号