WebService案例-实现天气查询

工程目录:

(一)创建Springboot项目并引入依赖

<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.1.12</version>
</dependency>

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

(二)创建实体类

@Data
public class Weather implements Serializable {

    private Long weatherId;

    private String city;

    private String weather;

    public Weather() {
    }

    public Weather(Long weatherId, String city, String weather) {
        this.weatherId = weatherId;
        this.city = city;
        this.weather = weather;
    }
}

(三)编写WebService需要的服务接口和实现

@WebService(name = "WeatherService", // 暴露服务名称
        targetNamespace = "http://service.wonder4work.com"// 命名空间,一般是接口的包名倒序
)
public interface WeatherService {

    /**
     * 查询所有的天气信息
     * @return
     * @author xiezengcheng
     */
    List<Weather> listAllCityWeather();

}
@Service
@WebService(serviceName = "WeatherService", // 与接口中指定的name一致
        targetNamespace = "http://service.wonder4work.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.wonder4work.webserviceserver.service.WeatherService"// 接口地址
)
public class WeatherServiceImpl implements WeatherService {


    @Override
    public List<Weather> listAllCityWeather() {

        Weather fzWeather = new Weather(1L, "福州", "晴");
        Weather xmWeather = new Weather(2L, "厦门", "多云");
        Weather shWeather = new Weather(3L, "上海", "雷阵雨");

        List<Weather> weatherList = new ArrayList<>();
        weatherList.add(fzWeather);
        weatherList.add(xmWeather);
        weatherList.add(shWeather);

        return weatherList;
    }
}

(四)编写Server端配置类暴露服务接口

@Configuration
public class CxfConfig {

    @Autowired
    private WeatherService weatherService;


    @Bean
    public ServletRegistrationBean exfServletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

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

    @Bean
    public WeatherService weatherService() {
        return weatherService;
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService());
        endpoint.publish("/api");
        // 添加拦截器 实现限流和安全可靠性
        endpoint.getInInterceptors().add(new AuthInterceptor("root", "root"));

        return endpoint;
    }

}

(五)启动项目,访问接口请求WSDL文件

(六)创建客户端Springboot项目,并引入相关依赖

(因为项目使用父子模块,所以不需要重新引入依赖)

(七)客户端请求服务方式一

@RestController
@RequestMapping("/weather")
public class WeatherController {

    /**
     * webservice接口地址
     */
    private static final String ADDRESS = "http://localhost:8080/services/api?wsdl";

    /**
     * 第一种调用方式
     * @return
     */
    @GetMapping
    public List<Weather> listAllCityWeather() {

        try {

            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(ADDRESS);
            //添加用户名密码拦截器
            //jaxWsProxyFactoryBean.getOutInterceptors().add(new LoginInterceptor("root","admin"));;
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(WeatherService.class);
            // 创建一个代理接口实现
            WeatherService weatherService = (WeatherService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            return weatherService.listAllCityWeather();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Weather>();
        }

    }

}

(八)客户端请求服务方式二

  1. 使用JDK自带命令解析wsdl文件得到实体类

wsimport -keep -p com.wonder4work.webserviceclient.client -d d:\sql http://localhost:8080/services/api?wsdl

     2. 将实体类拷贝到项目工程对应文件下

    3. 创建测试类

@Slf4j
public class ClientTest {


    public static void main(String[] args) {


        WeatherService weatherService = new WeatherService_Service()
                .getWeatherServiceImplPort();

        List<Weather> weatherList = weatherService.listAllCityWeather();

        weatherList.forEach(weather -> {
            log.debug(weather.toString());
        });
    }

}

(九)服务端知识点补充(可以自定义拦截器实现服务接口的权限拦截和访问控制)

源码获取:https://github.com/haidilaohotpot/subjects/tree/master/api/webservice

猜你喜欢

转载自blog.csdn.net/qq_41750725/article/details/109011855