使用jdk1.8完成了一个小示例,记录一下。springboot使用的2.7.15版本。
服务端
domain
package com.example.wsserver.domain;
public class Zonecode {
public Zonecode(String code, String name) {
this.code = code;
this.name = name;
}
private String code;
private String name;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Zonecode{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
'}';
}
}
服务
package com.example.wsserver.service;
import com.example.wsserver.domain.Zonecode;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
@WebService
public class ZonecodeService {
public List<Zonecode> queryCode() {
List<Zonecode> codes = new ArrayList<>();
Zonecode bj = new Zonecode("110000", "北京市");
Zonecode tj = new Zonecode("120000", "天津市");
Zonecode hb = new Zonecode("130000", "河北");
Zonecode sx = new Zonecode("140000", "山西");
codes.add(bj);
codes.add(tj);
codes.add(hb);
codes.add(sx);
return codes;
}
}
开放服务
package com.example.wsserver;
import com.example.wsserver.service.ZonecodeService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.xml.ws.Endpoint;
@SpringBootApplication
public class WsServerApplication {
public static void main(String[] args) {
SpringApplication.run(WsServerApplication.class, args);
Endpoint.publish(
"http://localhost:8081/WebServiceExample/queryCode",
new ZonecodeService());
}
}
服务地址:
http://localhost:8081/WebServiceExample/queryCode?wsdl
客户端
生成代码
wsimport -keep -p com.example.client.zonecode http://localhost:8081/WebServiceExample/queryCode?wsdl
代码结构
测试代码
@RestController
public class ZoneController {
@GetMapping("/codes")
public List<Zonecode> codes() {
ZonecodeService zonecodeService = new ZonecodeServiceService().getZonecodeServicePort();
return zonecodeService.queryCode();
}
}
报错
经排查是因为名称相同导致,所以需要修改名称,做如下处理: