gpt4 book ai didi

java - 在 JSF 中调用 JAX-WS

转载 作者:行者123 更新时间:2023-11-30 11:28:58 26 4
gpt4 key购买 nike

我已经使用 wsimport 将 WSDL 文件转换为 java

它生成两个文件

ICalculator

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


@WebService(name = "ICalculator", targetNamespace = "http://ws.test.ews/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface ICalculator {


/**
*
* @param arg2
* @param arg1
* @param arg0
* @return
* returns int
*/
@WebMethod(operationName = "PerformCalculation")
@WebResult(partName = "return")
@Action(input = "http://ws.test.ews/ICalculator/PerformCalculationRequest", output = "http://ws.test.ews/ICalculator/PerformCalculationResponse")
public int performCalculation(
@WebParam(name = "arg0", partName = "arg0")
int arg0,
@WebParam(name = "arg1", partName = "arg1")
int arg1,
@WebParam(name = "arg2", partName = "arg2")
String arg2);

}

CalculatorImplService

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;



@WebServiceClient(name = "CalculatorImplService", targetNamespace = "http://ws.test.ews/", wsdlLocation = "http://localhost:9998/ws/calculator?wsdl")
public class CalculatorImplService
extends Service
{

private final static URL CALCULATORIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException CALCULATORIMPLSERVICE_EXCEPTION;
private final static QName CALCULATORIMPLSERVICE_QNAME = new QName("http://ws.test.ews/", "CalculatorImplService");

static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:9998/ws/calculator?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
CALCULATORIMPLSERVICE_WSDL_LOCATION = url;
CALCULATORIMPLSERVICE_EXCEPTION = e;
}

public CalculatorImplService() {
super(__getWsdlLocation(), CALCULATORIMPLSERVICE_QNAME);
}

public CalculatorImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), CALCULATORIMPLSERVICE_QNAME, features);
}

public CalculatorImplService(URL wsdlLocation) {
super(wsdlLocation, CALCULATORIMPLSERVICE_QNAME);
}

public CalculatorImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, CALCULATORIMPLSERVICE_QNAME, features);
}

public CalculatorImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}

public CalculatorImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}

/**
*
* @return
* returns ICalculator
*/
@WebEndpoint(name = "CalculatorImplPort")
public ICalculator getCalculatorImplPort() {
return super.getPort(new QName("http://ws.test.ews/", "CalculatorImplPort"), ICalculator.class);
}

/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns ICalculator
*/
@WebEndpoint(name = "CalculatorImplPort")
public ICalculator getCalculatorImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://ws.test.ews/", "CalculatorImplPort"), ICalculator.class, features);
}

private static URL __getWsdlLocation() {
if (CALCULATORIMPLSERVICE_EXCEPTION!= null) {
throw CALCULATORIMPLSERVICE_EXCEPTION;
}
return CALCULATORIMPLSERVICE_WSDL_LOCATION;
}

}

现在我想在 JSF 中调用这个 WEB SERVICE。页面(JSF)发送三个参数并得到结果。

JSF 页面

<h:body>
<h3>Calculator</h3>
<h:form>
<h:inputText value="#{?}"></h:inputText> //for @para1
<h:inputText value="#{?}"></h:inputText> //for @para2
<h:inputText value="#{?}"></h:inputText> //for @para3
<h:commandButton value="Calculate" action="?"></h:commandButton>
</h:form>
<h:panelGroup">
<h4>Results</h4>
<table>
<tr><td>${?}</td></tr>
</table>
</h:panelGroup>
</h:body>

我可以在 CalculatorImplService 中使用 @ManagedBean @SessionScoped 吗?更新我!

如何在 JSF 中调用此 Web 服务?

最佳答案

使用 @WebServiceRef 注释在您的托管 bean 中引用 web 服务 stub 类 (CalculatorImplService)。使用 MyManagedBean 的托管 bean 类 您的代码的粗略示例如下:

@Named("myBean")
@RequestScoped
public class MyManagedBean{

@WebServiceRef(wsdlLocation="http://localhost:9998/ws/calculator?wsdl",value=CalculatorImplService.class)
ICalculator calculatorService;


public void calculate(int arg1,int arg2){
calculatorService.calculate();
}

}

在这里,我使用了 CDI @Named 注释。如果愿意,您可以自由使用 @ManagedBean

关于java - 在 JSF 中调用 JAX-WS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com