java soap_在java和SOAPUI中使用Web服务

我正在使用

java中的web服务.此服务使用UserNameToken,Timestamp和Signature来实现消息级安全性.我已经获得了SoapUI项目文件(xml)来尝试从服务中获取数据.在SoapUI中一切正常.

现在,当我尝试使用相同的soapUI文件生成工件时,我收到一条消息“Receiver Policy falsified”.为什么我可以连接到soapUI中的服务,但我不能在java中?

所以我发现我没有使用传输层安全性发送密钥库.如何在SOAP请求中发送它.我在SSL设置中的SOAP UI中做了类似的设置,它在那里工作.

这是我在java中的SOAP请求代码

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

SOAPConnection soapConnection = soapConnectionFactory.createConnection();

String url = "https://abc.org/test/ApplicantInformationService"; // Test System Web Service URL

SSLSocketFactory sslSocketFactory = getSSLSocketFactory();

conn = urlOnly.openConnection();

if (conn instanceof HttpsURLConnection) {

((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);

((HttpsURLConnection) conn).setRequestMethod("POST");

}

conn.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");

conn.setDoOutput(true);

SOAPMessage message = createSOAPRequest(user);

ByteArrayOutputStream os1 = new ByteArrayOutputStream();

message.writeTo(os1);

String requestXml = new String(os1.toByteArray());

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());

out.write(requestXml);

out.flush();

if(out != null){

out.close();

}

String line = "";

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String responseNewWay = "";

while ((line = in.readLine()) != null) {

responseNewWay = responseNewWay + line;

}

//SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(user), url);

//soapConnection.close();

//ByteArrayOutputStream os = new ByteArrayOutputStream();

//soapResponse.writeTo(os);

//String responseXml = new String(os.toByteArray());

而SSLFactory Code就是这样的

private static SSLSocketFactory getSSLSocketFactory()

throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, IOException, KeyManagementException{

SSLSocketFactory sslsf = null;

String keyStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";

String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();

String keyStoreType = "JKS";

String keyStorePassword = "mypassword";

String trustStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";

String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath();

String trustStorePassword = "mypassword";

String alias = "clientcertificate";

Properties systemProps = System.getProperties();

systemProps.put("javax.net.ssl.keyStore", keyStorePath);

systemProps.put("javax.net.ssl.keyStorePassword", keyStorePassword);

systemProps.put("javax.net.ssl.keyStoreType", keyStoreType);

systemProps.put("javax.net.ssl.trustStore", trustStorePath);

systemProps.put("javax.net.ssl.trustStoreType", "JKS");

systemProps.put("javax.net.ssl.trustStorePassword", trustStorePassword);

System.setProperties(systemProps);

KeyManager[] keyManagers = createKeyManagers(keyStoreFileName,keyStorePassword, alias);

TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword);

sslsf = initItAll(keyManagers, trustManagers);

return sslsf;

}

我得到的错误SOAP响应是这样的

soapenv:Receiver

Policy Falsified

https://abc.org/test/ApplicantInformationService

status="Service Not Found. The request may have been sent to an invalid URL, or intended for an unsupported operation." xmlns:l7="http://www.layer7tech.com/ws/policy/fault"/>

现在我收到了这个错误

Server returned HTTP response code: 500 for URL: https://abc.org/test/ApplicantInformationService

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了许多方式来编写和使用SOAP Web服务。下面是一些基本的步骤: 1. 导入Spring Web Services库 首先,需要在项目导入Spring Web Services库。可以使用Maven或Gradle等构建工具添加以下依赖项: ``` <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> ``` 2. 创建WSDL Web服务描述语言(WSDL)是一种XML格式,用于描述Web服务的接口、数据类型和协议。可以使用Spring Web Services自动生成WSDL文件。 在Spring,可以使用`@Endpoint`注解创建一个端点类,并使用`@PayloadRoot`注解指定请求的XML命名空间和元素名称。例如: ``` @Endpoint public class UserEndpoint { private static final String NAMESPACE_URI = "http://example.com/users"; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getUserRequest") @ResponsePayload public GetUserResponse getUser(@RequestPayload GetUserRequest request) { // 处理请求并返回响应 } } ``` 3. 配置Spring Web Services 可以使用Spring配置文件来配置Spring Web Services。以下是一个简单的配置文件示例: ``` <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="contextPath" value="com.example.users"/> </bean> <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/> <bean id="userEndpoint" class="com.example.users.UserEndpoint"/> <bean class="org.springframework.ws.transport.http.WebServiceExporter"> <property name="messageFactory" ref="messageFactory"/> <property name="port" value="8080"/> <property name="serviceMappings"> <props> <prop key="http://example.com/users">userEndpoint</prop> </props> </property> <property name="defaultEndpoint" ref="userEndpoint"/> </bean> ``` 在这个配置文件,我们定义了一个JAXB Marshaller用于将Java对象转换为XML,一个SOAP消息工厂用于创建SOAP消息,以及一个Web服务导出器用于将Web服务公开在HTTP端口8080上。 4. 测试Web服务 现在,可以使用SOAP客户端来测试Web服务。可以使用许多不同的SOAP客户端工具,例如SOAPUI或Apache CXF。以下是一个使用Apache CXF的简单Java客户端示例: ``` JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(UserService.class); factory.setAddress("http://localhost:8080/users"); UserService client = (UserService) factory.create(); GetUserRequest request = new GetUserRequest(); request.setId(1); GetUserResponse response = client.getUser(request); ``` 在这个示例,我们创建了一个JAX-WS代理工厂,并使用它创建了一个Web服务客户端。然后,我们创建一个请求对象并调用Web服务方法。 这就是使用Spring编写和使用SOAP Web服务的基本步骤。当然,这只是一个简单的示例,实际应用可能需要更复杂的配置和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值