如何使用JAX-WS将后端方法中的异常消息解析为SOAP响应消息?
我正在尝试使用 JAX-WS 和 Tomcat 将一种方法公开为后端的 Web 服务。 后端方法如下所示(在 CompanyFacade 类中):
public Company findCompanyById(Long id) throws Exception{
Company c = null;
try{
if(id == null){
throw new Exception("Failed to get company ID");
}
c = baseFacade.load(Company.class, id);
if(c == null || c.getId() == null){
throw new Exception("No company found");
}
}
catch(Exception e){
throw new Exception(e.getMessage());
}
finally{
return c;
}
}
至于 webservice 类,有一个 WebMethod 调用上述方法:
@WebMethod(operationName = "findCompanyById", action = "urn:findCompanyById")
@WebResult(name = "Company")
public Company findCompanyById(@WebParam(name = "id") Long id) throws Exception{
return CompanyFacade.findCompanyById(id);
}
是我收到的响应消息,应该有异常消息:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findCompanyByIdResponse xmlns:ns2="http://site.com/api/ws"/>
</S:Body>
</S:Envelope>
下面 唯一的问题是关于异常(例如,找不到公司),异常消息可以显示在 Tomcat 控制台中,但不能显示在 SOAP 响应消息中,似乎 WebMethod 中调用的方法没有返回任何异常。所以问题是:如何将后端方法中的异常消息解析为 SOAP 响应消息或者是否还有其他设计模式?提前致谢!
I am trying to expose one method as webservice from backend using JAX-WS with Tomcat.
The backend method is something like below (in CompanyFacade class):
public Company findCompanyById(Long id) throws Exception{
Company c = null;
try{
if(id == null){
throw new Exception("Failed to get company ID");
}
c = baseFacade.load(Company.class, id);
if(c == null || c.getId() == null){
throw new Exception("No company found");
}
}
catch(Exception e){
throw new Exception(e.getMessage());
}
finally{
return c;
}
}
As for the webservice class, there's a WebMethod invokes the above method:
@WebMethod(operationName = "findCompanyById", action = "urn:findCompanyById")
@WebResult(name = "Company")
public Company findCompanyById(@WebParam(name = "id") Long id) throws Exception{
return CompanyFacade.findCompanyById(id);
}
Below is the respond message I got, which is supposed to have the exception message:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findCompanyByIdResponse xmlns:ns2="http://site.com/api/ws"/>
</S:Body>
</S:Envelope>
The webservice works fine, the only problem is about the exception (e.g. No company found), the exception messages can be displayed in the Tomcat console, but not in the SOAP response message, it seems that the invoked method in the WebMethod doesn't return any exception. So the question is: how to parse the exception message from the method in backend to the SOAP response message OR is there any other design pattern? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SOAP 不处理异常——它只知道 SOAP 错误。在您的例子中,findCompanyById 方法引发的异常被包装在 SOAPFault 中并返回给用户。
您必须创建一个用
@WebFault
注解的类,并且该类必须扩展Exception
- 这是您的服务应该抛出的异常。 Web 服务不知道运行时异常,因此从服务中抛出未经检查的异常不会产生任何效果。或者,您可以创建一个FaultBean,可以在@WebFault注释中指定它,并且将携带有关特定错误的信息。您包含在FaultBean 中的字段将映射到 SOAP 响应的
detail
标记中的字段,而异常消息将映射到 SOAP 响应的faultstring
标记。您的 SOAP 响应。示例如下:
SOAP doesn't deal with exceptions - it only knows about SOAP faults. In your case, the exception thrown by the findCompanyById method is wrapped in a SOAPFault and thrown back to the user.
You must create a class annotated with
@WebFault
and that class must extendException
- this is the exception that your service should throw. Web Services don't know about runtime exceptions, so throwing unchecked exceptions from your service will not have any effect.Alternatively, you can create a FaultBean, which can be specified in
@WebFault
annotation, and will carry information about the specific error. The fields that you include in the FaultBean will be mapped to fields in thedetail
tag of the SOAP response, whilst the message of your exception will be mapped to thefaultstring
tag in your SOAP response.An example follows: