有少数@WebService
OpenEJB 示例 zip 文件中的单元测试示例。你想要的一切都应该工作正常。
- 简单网络服务 http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.4/examples/simple-webservice/
- 网络服务附件 http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.4/examples/webservice-attachments/
- 网络服务安全 http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.4/examples/webservice-security/
- webservice-ws-安全 http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.4/examples/webservice-ws-security/
webservice-security 示例听起来与您想要的完全一样。网上使用的版本@RolesAllowed
让容器进行安全检查而不是在代码中进行,但可以在代码中检查原理。这是该示例的稍微修改后的版本,对我来说没有任何问题。
The bean
@DeclareRoles(value = {"Administrator"})
@Stateless
@WebService(
portName = "CalculatorPort",
serviceName = "CalculatorWsService",
targetNamespace = "http://superbiz.org/wsdl",
endpointInterface = "org.superbiz.calculator.CalculatorWs")
public class CalculatorImpl implements CalculatorWs, CalculatorRemote {
@Resource
private WebServiceContext webServiceContext;
@RolesAllowed(value = {"Administrator"})
public int sum(int add1, int add2) {
// maybe log the principal or something -- prints "jane" in the test
System.out.print(webServiceContext.getUserPrincipal());
return add1 + add2;
}
@RolesAllowed(value = {"Administrator"})
public int multiply(int mul1, int mul2) {
return mul1 * mul2;
}
}
The Test
public class CalculatorTest extends TestCase {
private InitialContext initialContext;
protected void setUp() throws Exception {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
properties.setProperty("openejb.embedded.remotable", "true");
initialContext = new InitialContext(properties);
}
/**
* Create a webservice client using wsdl url
*
* @throws Exception
*/
public void testCalculatorViaWsInterface() throws Exception {
URL url = new URL("http://127.0.0.1:4204/CalculatorImpl?wsdl");
QName calcServiceQName = new QName("http://superbiz.org/wsdl", "CalculatorWsService");
Service calcService = Service.create(url, calcServiceQName);
assertNotNull(calcService);
CalculatorWs calc = calcService.getPort(CalculatorWs.class);
((BindingProvider) calc).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "jane");
((BindingProvider) calc).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "waterfall");
assertEquals(10, calc.sum(4, 6));
assertEquals(12, calc.multiply(3, 4));
}
}
图书馆
如果使用maven,请切换你的普通openejb-core
依赖于openejb-cxf
像这样。这会将 Apache CXF 和 OpenEJB/CXF 集成代码添加到您的类路径中。
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-cxf</artifactId>
<version>3.1.4</version>
<scope>test</scope>
</dependency>
如果不使用 Maven,最简单的方法是添加来自lib/
OpenEJB zip 文件的目录。