2021-12-16_WebService_step02__Java基础之IO流(系统信息)_冰雹猜想(简单未记录)

一、web service step02

4.1.2 添加Apache CXF依赖

pom.xml

4.1.3 写服务的接口

对外发布服务的接口:

package com.itheima.service;

import javax.jws.WebService;

/**
 * 对外发布服务的接口
 * 对于对外发布服务的接口,需要@WebService注解,用来标识一个web service接口
 * @author konglc
 * @date 2021/12/16 7:54
 */
@WebService
public interface HelloService {
    /**
     * 对外发布服务的接口的方法
     */
    public String sayHello(String name);
}

接口的实现类:

package com.itheima.service.impl;

import com.itheima.service.HelloService;

/**
 * 接口的实现
 * @author konglc
 * @date 2021/12/16 7:57
 */
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return name + ",Welecom to Itheima!";
    }
}

4.1.4 发布服务

package com.itheima;

import com.itheima.service.impl.HelloServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
 * @author konglc
 * @date 2021/12/16 8:00
 */
public class Server {
    public static void main(String[] args) {
        // 发布服务的工厂
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();

        // 设置服务地址
        factory.setAddress("http://localhost:8000/ws/hello");

        // 设置服务类
        factory.setServiceBean(new HelloServiceImpl());

        // 发布服务
        factory.create();

        System.out.println("发布服务成功,端口8000......");
    }
}

在编辑器的Server.java内,右击“Run 'Service.main()'”。运行结果:

发布服务成功,端口8000......

4.1.5 访问wsdl说明书

在服务地址后面加“?wsdl”:

http://localhost:8000/ws/hello?wsdl

4.2 客户端

4.2.1 创建项目

4.2.2 添加依赖

4.2.3 服务接口

package com.itheima.service;

import javax.jws.WebService;

/**
 * 对外发布服务的接口
 * 对于对外发布服务的接口,需要@WebService注解,用来标识一个web service接口
 * @author konglc
 * @date 2021/12/16 13:25
 */
@WebService
public interface HelloService {
    /**
     * 对外发布服务的接口的方法
     */
    public String sayHello(String name);
}

4.2.4 远程访问服务端

package com.itheima;

import com.itheima.service.HelloService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/**
 * @author konglc
 * @date 2021/12/16 13:28
 */
public class Client {
    public static void main(String[] args) {
        // 服务接口访问地址:http://localhost:8000/ws/hello

        // 创建cxf代理工厂
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        // 设置远程访问服务端地址
        factory.setAddress("http://localhost:8000/ws/hello");

        // 设置接口类型
        factory.setServiceClass(HelloService.class);

        // 对接口生成代理对象
        HelloService helloService = factory.create(HelloService.class);

        // 代理对象类型 class com.sun.proxy.$Proxy34
        // [Java代理:1.静态代理;2.动态代理(jdk接口代理、cglib子类代理)]
        // 形如$Proxy34,是jdk接口代理
        // 形如$CGLIB123,是cglib子类代理
        System.out.println(helloService.getClass());

        // 远程访问服务端方法
        String content = helloService.sayHello("Jet");
        System.out.println(content);
    }
}

在编辑器的Client.java内,右击“Run 'Client.main()'”。运行结果:

class com.sun.proxy.$Proxy34
Jet,Welecom to Itheima!

 二、Java视频:P249:IO流(系统信息)

import java.util.*;
import java.io.*;

class SystemInfo {
    public static void main(String[] args) throws IOException {
        Properties prop = System.getProperties();

        // System.out.println(prop);
        // prop.list(System.out);
        prop.list(new PrintStream("systeminfo.txt"));
    }
}

三、算法题目problem220:冰雹猜想(简单未记录代码)。

posted on 2021-12-16 22:06  平凡力量  阅读(66)  评论(0编辑  收藏  举报