我有一个运行良好的网络服务的Metro客户端。 但是,我们无法访问实验室环境中的服务,因此我编写了一个用于测试的模拟服务。我在理解端点以及如何设置端点时遇到问题。
我将 URL 传递到客户端构造函数中,并在请求上下文中进行设置,如下所示:
// Set the service port URL
BindingProvider bindingProvider = ((BindingProvider) port);
Map<String, Object> context = bindingProvider.getRequestContext();
context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
对于我的模拟服务,当我单击应用程序列表 (http://myserver:80/MySoapService( 中的"启动"链接时,我使用的是 Glassfish 提供给我的战争 URL。
代码是从提供给我们的 wsdl 生成的。 Web 服务界面如下所示:
@WebService(name = "My_Soap_Service", targetNamespace = "urn:My.DataEntityModel")
@XmlSeeAlso({
ObjectFactory.class
})
public interface MySoapService {
@WebMethod(operationName = "Ping", action = "urn:My.DataEntityModel/Ping")
@WebResult(name = "PingResult", targetNamespace = "urn:My.DataEntityModel")
@RequestWrapper(localName = "Ping", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.Ping")
@ResponseWrapper(localName = "PingResponse", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.PingResponse")
public String ping(
@WebParam(name = "inputString", targetNamespace = "urn:My.DataEntityModel")
String inputString);
@WebMethod(operationName = "ProcessRecordResult", action = "urn:My.DataEntityModel/ProcessRecordResult")
@WebResult(name = "ProcessRecordResultResult", targetNamespace = "urn:My.DataEntityModel")
@RequestWrapper(localName = "ProcessRecordResult", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.ProcessRecordResult")
@ResponseWrapper(localName = "ProcessRecordResultResponse", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.ProcessRecordResultResponse")
public String ProcessRecordResult(
@WebParam(name = "recordStatusXML", targetNamespace = "urn:My.DataEntityModel")
String recordStatusXML);
@WebMethod(operationName = "ProcessBatchResult", action = "urn:My.DataEntityModel/ProcessBatchResult")
@WebResult(name = "ProcessBatchResultResult", targetNamespace = "urn:My.DataEntityModel")
@RequestWrapper(localName = "ProcessBatchResult", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.ProcessBatchResult")
@ResponseWrapper(localName = "ProcessBatchResultResponse", targetNamespace = "urn:My.DataEntityModel", className = "dataentitymodel.ProcessBatchResultResponse")
public String processBatchResult(
@WebParam(name = "batchStatusXML", targetNamespace = "urn:My.DataEntityModel")
String batchStatusXML);
}
我的网站.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class>
</listener>
<servlet>
<servlet-name>MySoapService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySoapService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
我在设置 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="MyService"
implementation="my.package.MySoapServiceImpl"
url-pattern="/"
wsdl-location="WEB-INF/wsdl/MySoapService.wsdl"/>
</endpoints>
但是我的客户得到了一个描述该服务的"Web 服务"HTML页面。我无法找到 sun-jaxws.xml 文件的任何实用信息/示例,但我认为我需要为 Web 服务中的每个方法提供一个端点。 所以我把它改成这样:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="Ping"
implementation="my.package.MySoapServiceImpl"
url-pattern="/ping"
wsdl-location="WEB-INF/wsdl/MySoapService.wsdl"/>
<endpoint
name="ProcessRecord"
implementation="my.package.MySoapServiceImpl"
url-pattern="/processRecordResult"
wsdl-location="WEB-INF/wsdl/MySoapService.wsdl"/>
<endpoint
name="ProcessBatch"
implementation="my.package.MySoapServiceImpl"
url-pattern="/processBatchResult"
wsdl-location="WEB-INF/wsdl/MySoapService.wsdl"/>
</endpoints>
现在,当我的客户端尝试访问该服务时,我收到 404 错误。
我不知道我是否错误地设置了 sun-jaxws.xml 和/或 web.xml 文件,在客户端中使用了错误的 URL 或完全不同的东西。
有人可以告诉我我做错了什么和/或指出我以易于理解的方式解释这一点的资源吗?
我让它工作了,如果这对其他人有用,这是我的配置......
网络.xml
<web-app>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class>
</listener>
<servlet>
<servlet-name>MySoapService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySoapService</servlet-name>
<url-pattern>/service</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
太阳-贾克斯.xml
尽管我找到了很多示例/博客,指出 sun-jaxws.xml 文件中的port
和service
元素是可选的,但没有它们,我就无法让它工作。 另外,我发现一些帖子指出,如果在Glassfish上部署,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="MyService"
interface="my.package.MySoapServiceInterface"
implementation="my.package.MySoapServiceImpl"
service="{urn:my.xmlns.tns.urn}My_Soap_Service"
port="{urn:my.xmlns.tns.urn}My_Port_Type"
url-pattern="/service"
wsdl="WEB-INF/wsdl/MyService.wsdl"
</endpoints>
Web Service Impl 类
我发现有必要在WebService注释中包含endpointInterface
:
@WebService(endpointInterface="my.package.MySoapServiceInterface")
public class MySoapServiceImpl implements MySoapServiceInterface
当然,每个 Web 服务方法都需要用 WebMethod
注释:
@WebMethod
public String doSomething(String string)