Java 中的JAX-WS Webservice类初始化块
有点网络服务新手,但这里有一个棘手的问题,我正在努力寻找更好的实现方法。请参阅下面的代码:是否有一种方法可以只调用一次,而不必在每个webservice方法上调用setClientIPAddress方法? i、 e.我尝试了以下方法:Java 中的JAX-WS Webservice类初始化块,java,web-services,jakarta-ee,jax-ws,websphere-7,Java,Web Services,Jakarta Ee,Jax Ws,Websphere 7,有点网络服务新手,但这里有一个棘手的问题,我正在努力寻找更好的实现方法。请参阅下面的代码:是否有一种方法可以只调用一次,而不必在每个webservice方法上调用setClientIPAddress方法? i、 e.我尝试了以下方法: // initialisation block { WSBean wsBean = new WSBean(); wsBean.setClientIPAddress(getClientIPAdd); } 这编译正常,但我得到一个运行时错误。Webse
// initialisation block
{
WSBean wsBean = new WSBean();
wsBean.setClientIPAddress(getClientIPAdd);
}
这编译正常,但我得到一个运行时错误。Webservice类似乎不喜欢初始化块
@javax.jws.WebService(targetNamespace = "http://baseentity.com/", serviceName = "WSBeanService", portName = "WSBeanPort", wsdlLocation = "WEB-INF/wsdl/WSBeanService.wsdl")
public class WSBeanDelegate {
WSBean wsBean = new WSBean();
public String getBaseInfoList(String baseID) {
wsBean.setClientIPAddress(getClientIPAdd); //
return wsBean.getBaseInfoList(transactionID);
}
public String getBaseEntityInfo(String entityID) {
wsBean.setClientIPAddress(getClientIPAdd);
return wsBean.getBaseEntityInfo(entityID);
}
@WebMethod
private String getClientIPAdd()
{
MessageContext mc = this.wsContext.getMessageContext();
HttpServletRequest req = (HttpServletRequest)mc.get("javax.xml.ws.servlet.request");
return req.getRemoteAddr();
}
我尝试过使用@postcontract,如下所示:
@PostContruct
private void init()
{
wsBean.setClientIPAddress(getClientIPAdd);
}
但是我得到了以下错误:“带有私有修饰符的illegalaccessexception”
然而,将该方法声明为public还需要在bean/wsdl文件中定义相同的方法,这不是我想要做的。关于如何改进此代码有何建议
提前感谢。试试:
@PostContruct
@javax.jws.WebMethod(exclude=true)
public void init()
{
wsBean.setClientIPAddress(getClientIPAdd);
}
你不能在外面的另一个类中实现这个吗?然后,只有当静态变量clientAddressIP为null时,才必须设置它。。。。您可以设置它,以便在getClientAddressIP上检查它是否为null,然后设置它并返回。。。否则返回clientIPAddress。这是一个包含静态字符串clientIPAddress的单例类的好例子。。还有其他这样的变量Shi Nav,尝试了一些使用静态变量的方法,但是没有成功。当你尝试静态变量时发生了什么?在上面的问题中添加静态失败的详细信息。这将进一步帮助所有人,试图解决这个问题。在我看来,这是一个产品问题。PostConstruct应该可以在非公共方法上使用。完美地工作!!只需将方法更改为Public。谢谢乔纳森