cxf webservice客户端更改请求地址_SpringBoot整合Webservice 提供服务接口 - 服务端

09420fe2623297ad98cd999b3d671f8a.png

引入webservice需要使用jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.7</version>
</dependency>

书写接口和实现类

接口类

package org.andot.webservice.demo.server;

import org.andot.webservice.demo.entity.Person;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://andot.org/webservice/demo/server")
public interface TestApiService {
    @WebMethod
    Person insertPersonInfo(@WebParam(name = "PERSON", targetNamespace = "http://andot.org/webservice/demo/server") String person);
}

接口实现类

package org.andot.webservice.demo.server.provider;

import com.alibaba.fastjson.JSONArray;
import org.andot.webservice.demo.entity.Person;
import org.andot.webservice.demo.server.TestApiService;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.List;

@Component
@WebService(name = "testApiService",
    targetNamespace = "http://andot.org/webservice/demo/server",
    endpointInterface = "org.andot.webservice.demo.server.TestApiService",
    portName = "10000")
public class TestApiServiceImpl implements TestApiService {
    @Override
    public Person insertPersonInfo(String person) {
        List<Person> list = JSONArray.parseArray(person, Person.class);
        //TODO 逻辑处理
        return list.get(0);
    }
}

实体类

package org.andot.webservice.demo.entity;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {
    private Integer id;
    private String name;
    private String niceName;
    private Integer age;
    private Double height;
}

配置类

package org.andot.webservice.demo.start;

import org.andot.webservice.demo.server.TestApiService;
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.beans.factory.annotation.Qualifier;
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 TestWebService {
    @Bean
    public ServletRegistrationBean wsServlet(){
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Autowired
    private TestApiService testApiService;

    @Autowired
    @Qualifier(Bus.DEFAULT_BUS_ID)
    private SpringBus bus;

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, testApiService);
        endpoint.publish("/testApiService");
        return endpoint;
    }
}

访问地址

http://localhost:8080/ws/testApiService?wsdl

返回XML

使用POSTman进行测试

地址输入 http://localhost:8080/ws/testApiService

请求方式 POST

请求头 Context-Type:text/html

请求文本:

<?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>
    <insertPersonInfo xmlns="http://andot.org/webservice/demo/server">
      <PERSON>
        [{"name":"你好"},{"name":"你好2"}]
      </PERSON>
    </insertPersonInfo>
  </soap:Body>
</soap:Envelope>

详细说明:

(1)insertPersonInfo是方法名。

(2)PERSON是方法参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值