一、总体步骤:
1、配置tomcat的https连接
2、发布(接口/实现类/XML配置)
3、调用(书写一个启动类,当然也可以直接使用浏览器进行访问)
二、成型效果图:
三、具体代码:
1、配置tomcat的https连接
①生成密钥文件(tomcat.keystore 将其放在Tomcat的配置目录中并记录他的位置
D:\develop\tomcat\apache-tomcat-7.0.70-https\conf\tomcat.keystore
生成方法:
1)在cmd中输入命令:
keytool -genkey -alias tomcat-server -keyalg RSA -keypass changeit -storepass
changeit -validity 3600
2)按照提示一次输入相应的内容,最后一步输入“y”即可
3)在运行命令的目录下就生成了.keystore文件。密码为:123456。修改文件名,如: hello.keystore,防止程序不识别。
4)将hello.keystore文件拷贝到Tomcat的配置目录中。
②在Tomcat配置文件中打开server.xml,找到8443进行放开并添加
keystoreFile="\conf\tomcat.keystore"
keystorePass="123456"![在这里插入图片描述](https://img-blog.csdnimg.cn/20200820231859507.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjEzNzgxNQ==,size_16,color_FFFFFF,t_70#pic_center)
③检验是否配置成功,直接浏览器访问端口8443进行下列图片中步骤,出现Tomcat首页页面即成功,
若不成功需要反复检查秘钥文件名以及所在位置,还有就是Tomcat配置之前是否正常
2、发布(接口/实现类/XML配置)
我就直接上代码了
下面展示接口和实现类
package com.uuzz.sporttery;
public class User {
private String UserName;
public User() {}
public User(String UserName) {
this.UserName = UserName;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
}
package com.uuzz.sporttery;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface Hello {
String sayHi(@WebParam(name = "text") String text);
String sayHiToUser(User user);
String[] sayHiToUserList(List<User> userList);
}
package com.uuzz.sporttery;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface = "com.uuzz.sporttery.Hello", serviceName = "Hello")
public class HelloImpl implements Hello {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size() + 1, user);
return "Hello " + user.getUserName();
}
public String[] sayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i = 0;
for (User u : userList) {
result[i] = "Hello " + u.getUserName();
i++;
}
return result;
}
}
XML配置中sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="HelloImplWS" implementation="com.uuzz.sporttery.HelloImpl"
url-pattern="/helloImpl" />
</endpoints>
<!--http://localhost:8443/-->
<!--https://localhost:8443/-->
<!--https://localhost:8443/httpswebservice/helloImpl?wsdl-->
XML配置中web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>helloImpl</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloImpl</servlet-name>
<url-pattern>/helloImpl</url-pattern>
</servlet-mapping>
</web-app>
3、调用(书写一个启动类,当然也可以直接使用浏览器进行访问)
启动类
package com.uuzz.sporttery.client;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class HelloClientHttps {
public static void main(String[] args) throws ServiceException,
RemoteException {
System.setProperty("javax.net.ssl.keyStore",
"D:\\develop\\tomcat\\apache-tomcat-7.0.70-https\\conf\\tomcat.keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
System.setProperty("javax.net.ssl.trustStore",
"D:\\develop\\tomcat\\apache-tomcat-7.0.70-https\\conf\\tomcat.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
Service _service = new Service();
Call _call = (Call) _service.createCall();
_call.setTimeout(1000);
_call.setTargetEndpointAddress("https://localhost:8443/httpswebservice/helloImpl?wsdl");
QName opAddEntry = new QName("http://sporttery.uuzz.com/", "sayHi");
_call.setOperationName(opAddEntry);
_call.addParameter("text", XMLType.XSD_STRING, ParameterMode.IN);
_call.setReturnType(XMLType.XSD_STRING);
Object result = _call.invoke(new String[] { "My Friend" });
System.out.println(result);
}
}
四、最终验证:
先手动启动tomcat,在直接点击启动类就可以出现调用成功上述效果图,
当然你也可以直接用浏览器访问看是否调用成功
访问 https://localhost:8443/httpswebservice/helloImpl?wsdl出现下图即调用成功
五、总结:
①:tomcat配置https时确保秘钥文件以及位置ok
②:可能出现tomcat7和jdk8版本不兼容问题,可以通过添加注解或者使用高的版本操作
③:关于书写启动类有几个必要的包,可以参考上面提供进行比对
④:使用的web工程结合了maven,在引入java代码