提交 42c146a8 authored 作者: 邓砥奕's avatar 邓砥奕

更新代码

上级 6a89b31f
...@@ -24,6 +24,46 @@ ...@@ -24,6 +24,46 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<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.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>jakarta.jws</groupId>
<artifactId>jakarta.jws-api</artifactId>
<version>2.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package com.example.demo;
import org.springframework.stereotype.Repository;
import java.io.IOException;
@Repository
public interface WebSevice {
String setUser(String keySn,String userCode ,String userName,String overDate,String password) throws IOException;
}
package com.example.demo.WebSeviceConfig;
import com.example.demo.service.impl.Test;
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 com.example.demo.service.WebSevice;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
@Autowired
private WebSevice webSevice;
@Bean
public ServletRegistrationBean disServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webSevice);
endpoint.publish("/api");
return endpoint;
}
}
package com.example.demo.client;
import com.example.demo.service.WebSevice;
import com.example.demo.service.impl.Test;
import com.sun.xml.bind.v2.runtime.output.C14nXmlOutput;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class CxfClient {
public static void main(String[] args) {
CxfClient.main1();
}
public static void main1() {
try {
// 接口地址
String address = "http://localhost:8080/demo/api?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(WebSevice.class);
// 创建一个代理接口实现
WebSevice us = (WebSevice) jaxWsProxyFactoryBean.create();
// 数据准备
String keySn = "123456";
String userCode = "1";
String userName = "dengdiyi";
String overDate = "2025-10-10";
String password ="123456";
// 调用代理接口的方法调用并返回结果
String result = us.setUser(keySn,userCode,userName,overDate,password);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.io.IOException;
@WebService(targetNamespace = "http://service.demo.example.com")
public interface WebSevice {
@WebMethod
String setUser(String keySn, String userCode , String userName, String overDate, String password) throws IOException;
}
package com.example.demo.TestApi; package com.example.demo.service.impl;
import com.example.demo.WebSevice; import com.example.demo.service.WebSevice;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -10,50 +13,53 @@ import java.io.OutputStream; ...@@ -10,50 +13,53 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@WebService(serviceName = "MyService",targetNamespace = "http://service.demo.example.com",endpointInterface = "com.example.demo.service.WebSevice")
@Component
public class Test implements WebSevice { public class Test implements WebSevice {
@Override @Override
public String setUser(String keySn,String userCode ,String userName,String overDate,String password) throws IOException { public String setUser(String keySn,String userCode ,String userName,String overDate,String password) throws IOException {
//第一步:创建服务地址 // //第一步:创建服务地址
URL url = new URL("http://127.0.0.1:8080/iSignatureServer/services/KeyInfoService?wsdl"); // URL url = new URL("http://127.0.0.1:8080/iSignatureServer/services/KeyInfoService?wsdl");
//第二步:打开一个通向服务地址的连接 // //第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数 // //第三步:设置参数
//3.1发送方式设置:POST必须大写 // //3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST"); // connection.setRequestMethod("POST");
//3.2设置数据格式:content-type // //3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); // connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限, // //3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true); // connection.setDoInput(true);
connection.setDoOutput(true); // connection.setDoOutput(true);
String outPut = null; // String outPut = null;
//
//第四步:组织SOAP数据,发送请求 // //第四步:组织SOAP数据,发送请求
String soapXML = getXML(keySn,userCode,userName,overDate,password); // String soapXML = getXML(keySn,userCode,userName,overDate,password);
OutputStream os = connection.getOutputStream(); // OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes()); // os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印 // //第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode(); // int responseCode = connection.getResponseCode();
if (200 == responseCode) { // if (200 == responseCode) {
InputStream is = connection.getInputStream(); // InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is); // InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); // BufferedReader br = new BufferedReader(isr);
//
StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
String temp = null; // String temp = null;
while (null != (temp = br.readLine())) { // while (null != (temp = br.readLine())) {
sb.append(temp); // sb.append(temp);
} // }
outPut = sb.toString(); // outPut = sb.toString();
/** // /**
* 打印结果 // * 打印结果
*/ // */
// System.out.println(sb.toString()); //// System.out.println(sb.toString());
is.close(); // is.close();
isr.close(); // isr.close();
br.close(); // br.close();
} // }
os.close(); // os.close();
return outPut; // return outPut;
return "Hello World!";
} }
...@@ -76,4 +82,5 @@ public class Test implements WebSevice { ...@@ -76,4 +82,5 @@ public class Test implements WebSevice {
+ "</soap:Envelope>"; + "</soap:Envelope>";
return soapXML; return soapXML;
} }
} }
...@@ -3,15 +3,15 @@ package com.example.demo; ...@@ -3,15 +3,15 @@ package com.example.demo;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.service.WebSevice;
import java.io.IOException; import java.io.IOException;
@SpringBootTest @SpringBootTest
public class NewTest { public class NewTest {
@Autowired
private WebSevice webSevice;
@Test @Test
public void test1() throws IOException { public void test1() throws IOException {
com.example.demo.TestApi.Test test =new com.example.demo.TestApi.Test(); System.out.println(webSevice.setUser("12345678","1","dengdiyi","2025-10-10","123456"));
System.out.println(test.setUser("12345678","1","dengdiyi","2025-10-10","123456"));
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论