CDI Constructor Injection in a JAX-WS web service - web-services

I have a JAX-WS webservice deployed to a Java EE 6 container.
I would like to use CDI to inject session beans to this webservice. Up till now I have successfully been doing this using field injection but the company policy is to use constructor based injection. The webservice implementation is NOT a session bean. So is
#WebService
public class MyWebservice{
private ServiceA serviceA;
private ServiceB serviceB;
#Inject
public MyWebservice( ServiceA serviceA, ServiceB serviceB)
{
this.serviceA = serviceA;
this.serviceB = serviceB;
}
public void webMethod1()
{
serviceA.doSomething();
serviceB.doSomething();
}
}
acceptable to CDI?

Related

RESTful application without web.xml

I have created a sample project and used EJB 3.1 with a RESTful web service. In the sample I have a class which extends Application. I expect the class works like a servlet and dispatch requests to appropriate classes but it does not. When I use web.xml my sample project works fine. What is wrong with my sample project?
#ApplicationPath("/rest")
public class ApplicationServlet extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(UserWS.class);
return classes;
}
}
I use UserWS as a EJB session bean which exposes web service:
#Stateless
#LocalBean
#Path("/user")
public class UserWS {
private int count;
public UserWS() {
this.count=0;
}
#GET
#Path("/name/{username}")
public void getUserName(#PathParam("username") String username) {
count++;
System.out.println("count is:"+ count);
}
}
I'm afraid it won't be possible once JBoss 5.0 supports only Servlet 2.5. For more details, see here.
To avoid the web.xml deployment descriptor, you need a servlet container the supports at least Servlet 3.0.
So, what could you do to solve it?
These are the options that came up to my mind:
You could try upgrading the JBoss Web (Tomcat fork used by JBoss AS) as described here, but try that at you own risk.
Consider using a recent version of JBoss/WildFly.

Wildfly 9 Stateless session bean webservice not showing up in Web Service Endpoints

I'm quite new to the Wildfly server.
Currently I'm trying to expose a simple Stateless Session Bean as web service. I don't want to use the webModule to define servlet mappings for the web services beans. I want to keep that seperated.
I just want to expose a simple Stateless Session Bean as web service.
I used the wildfly-javaee7-webapp-ear-blank-archetype as starting point.
In the ejbModule i added a Stateless Session Bean with #WebService annotation.
The ejbModule is packaged in an .ear file, which is deployed to the WildFly Server 9. The deployment shows now error message.
I now expected to see some endpoints in the admin console under web service endpoints as the documentation(https://docs.jboss.org/author/display/WFLY9/JAX-WS+User+Guide) says. But I can't see any endpoints.
What I'm doing wrong ?
How can i access the generated WSDL file of the web service ?
What is the exact context root when ejb is packaged inside ear file?
Any hints are appreciated.
package eu.sample.testws.service;
import javax.ejb.Stateless;
import javax.jws.WebService;
/**
* Session Bean implementation class TestWSBean
*/
#Stateless
#WebService(serviceName="TestWSService", name="TestWSServiceName", portName="TestWSPortName", targetNamespace="http://sample.eu")
public class TestWSBean {
/**
* Default constructor.
*/
public TestWSBean() {
// TODO Auto-generated constructor stub
}
public String sayHello(){
return "Hello";
}
}

Cannot deploy web service in GlassFish using together #WebserviceProvider and #Stateless

I tried to deploy in GlassFish JAX-WS web service,
Here is a snippet of class were the web service is defined. Pay attention that I implemented Provider interface on EJB endpoint.
#Stateless(name = "HelloWorldEJBWS")
#WebServiceProvider(
portName = "HelloWorldWSPort",
serviceName = "HelloWorldWSService",
targetNamespace = "http://ivan.com/",
wsdlLocation ="HelloWorldEJBProvider.wsdl")
#ServiceMode(value = Service.Mode.PAYLOAD)
public class HelloWorldEJBWS implements Provider<Source> {
public Source invoke(final Source inRequestMessage) {
...
}
}
The problem is about the deploying the service in GlassFish (3.1.2.2) . F.
[#|2012-09-08T16:39:15.682-0400|INFO|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=20;_ThreadName=Thread-2;|EJB5181:Portable JNDI names for EJB HelloWorldEJBWS: [java:global/JAX-WS_GreetingEJBMutualAuthProvider/HelloWorldEJBWS, java:global/JAX-WS_GreetingEJBMutualAuthProvider/HelloWorldEJBWS!javax.xml.ws.Provider]|#]
[#|2012-09-08T16:39:15.792-0400|INFO|glassfish3.1.2|javax.enterprise.webservices.org.glassfish.webservices|_ThreadID=20;_ThreadName=Thread-2;|WS00019: EJB Endpoint deployed
JAX-WS_GreetingEJBMutualAuthProvider listening at address at http://ABRAMOV1:8088/HelloWorldWSService/com.ivan.wsejb.provider.HelloWorldEJBWS|#]
Even it shows the endpoint is deployed - is not . I can't reach this endpoint and it is not shown in GlassFish console.
For comparison I provide the log when I deployed the service using #WebService but not #WebServiceProvider
[#|2012-09-08T16:41:50.514-0400|INFO|glassfish3.1.2|javax.enterprise.webservices.org.glassfish.webservices|_ThreadID=22;_ThreadName=Thread-2;|WS00019: EJB Endpoint deployed
JAX-WS_GreetingEJBMutualAuth listening at address at http://ABRAMOV1:8088/HelloWorldEJBWSService/HelloWorldEJBWS|#]
In this case endpoint deployed correctly and everything is working fine.
Here is snipped of the code when I apply #WebService
#Stateless(name = "HelloWorldEJBWS")
#WebService()
public class HelloWorldEJBWS {
public String hello(final String inMessage) {
...
}
}
Did I do something wrong ?
I did everything right but was mislead by GlassFish. It could be a a bug...
When I deploy web service with endpoint implemented as servlet (second case) in the console I can see endpoint, but in case with endpoint implemented as EJB the endpoint did not appear in the console. But I could access the WSDL with a link http://localhost:8088/HelloWorldWSService/com.ivan.wsejb.provider.HelloWorldEJBWS?wsdl and ultimately tested web service with the client

Access EJB 3.0 as an EJB and as a web service on Websphere 7

I want to deploy an EJB 3.0 stateless bean to WAS7 so I can access it as an EJB through a local interface and also as a jax-ws web service.
My bean looks as following:
#Stateless
#WebService
public class UserManagerImpl implements UserManager {
public UserManagerImpl() {
}
#WebMethod
public String getName(){
return "UserName";
}
}
The problem is that if I package it into an EJB-JAR and deploy, it doesn't work as a web service on WAS-7.
The only working configuration for me is if I put the EJB-JAR into a EAR and put this EJB-JAR to a WAR that is also in the EAR, like this:
EAR/
|--EJB-JAR
|--WAR/
|WEB-INF/lib/
|EJB-JAR
So my bean is duplicated.
Is there any problem with this design? If so, is there a better solution?
If your application contains #WebService annotated EJBs, then you need to process the EAR with the endptEnabler tool shipped with WebSphere before deploying it. Note that this doesn't apply to #WebService annotated classes in Web modules.

Spring & Mockito - ignore transitive dependencies

I have a layered application that looks like this:
#PreAuthorize('isAuthenticated()')
#Controller
public class MyController {
#Autowired
MyService service;
}
#Service
public class MyService {
#Autowired
MyDao dao;
}
public interface MyDao {
}
#Repository
public class MyDaoImpl implements MyDao {
}
I want to test the AOP-dependent #PreAuthorize annotation, so I use the SpringJUnit4ClassRunner which creates a test AuthenticationManager and MyController.
If the #ContextConfiguration does not include any bean matching MyService, test initialization fails because it can't resolve the bean.
If I didn't need AOP, I would use Mockito test runner and inject Mockito.mock(MyService.class). But if I try and do it with spring runner, again my test fails because it can't resolve MyDao for the service (even though the service a mock).
I definitely don't want to mock the whole object graph. I'd rather it stopped on the mocked service. How can I do it?
Your MyService should implement an interface, and you should mock the interface instead of the class. Then you won't need a DAO implementation. You might also, perhaps, be running into a similar issue that I faced while trying to test a JAX-RS resource class in Jersey. The problem is how to deploy a single bean into a Spring container but mock its dependencies. I wrote a blog post on it that might help you out if this is the problem you're facing. In particular, the final solution may be of help.