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.
Related
Actully I am trying to test my Jersey web service with the jersey test framework. the web server I am using is Websphere 7 and java version 6. This is my project requirement I cannot upgrade the java version .
My issue is how to build the unit test for my web service . I want to test them on WebSphere but I am not sure how do I setup the environment for unit testing like junit .
more specifically I just need to call an URL from the test class and check the response. But how to call the URL from the test class on the websphere I am not getting direction for it.
Have a look at the documentation for Jersey Test Framework.
First thing you need is one of the Supported Containers dependencies. Any of those will pull in the core framework e.g.
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>
Then you need a test class that extends JerseyTest. You can override Application configure() to provide a ResourceConfig as well as any other providers or properties. For example
#Path("/test")
public class TestResource {
#GET
public String get() { return "hello"; }
}
public class TestResourceTest extends JerseyTest {
#Override
public Application configure() {
ResourceConfig config = new ResourceConfig();
config.register(TestResource.class);
}
#Test
public void doTest() {
Response response = target("test").request().get();
assertEquals(200, response.getStatus());
assertEquals("hello", response.readEntity(String.class));
}
}
You should visit the link provided to learn more and see more examples.
My application should show a list of stuff over RESTful WebServices.
This list of stuff is generated via an EJB which is deployed in JBoss AS 6 in a .jar.
My question is, how do I get this list of stuff in order to show it via Web Services?
If I try to inject #EJB in the stateless bean annotated with #WebService, and try to invoke the method that generates the list, I get an error saying that #EJB cannot be resolved (or imported). (fixed it, needed to modify maven dependencies).
Now I get a NullPointerException when I call the supposedly injected EJB...
I am using the webapp archetype from Maven, which contains an EAR, a JAR and a WAR.
I am at a total loss here, been trying to figure this for almost a week.
Thanks in advance!
--EDIT-- code and typo fixed
This is my stateless bean which SHOULD be exposed as a WebService (according to:
http://www.adam-bien.com/roller/abien/entry/restful_calculator_with_javascript_and )
#Stateless
#Path("/MyRESTApplication")
public class HelloWorldResource {
#EJB
private TestBean testBean;
#GET()
#Produces("text/plain")
public String sayHello(String name) {
return testBean.doMoreStuff();
}
}
the doMoreStuff() function simply returns "HELLO!".
i have a problem deploying my webservice to Weblogic 11g.
JAVA: JRockit 1.6.x
I need to run a method on webservice deployment and i made this code:
#Singleton
#Startup
public class StartupBean {
Logger logger = Logger.getLogger(StartupBean.class);
#PostConstruct
private void postConstruct() {
logger.error("WS started.");
}
#PreDestroy
private void preDestroy() {
logger.error("WS stoped.");
}
}
without any additional xml config.
It works normally on 12c but i need it on 11g.
What is the workaround?
Thanks
No, you can't do this in pre-3.1-EJB without XML config.
The common practice in EJB 3.0 for implementing a #Startup bean was to instantiate in in a servlet, which is configured to load on startup in web.xml. If you need it just for log4j you can initialize it directly from such servlet.
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.
EJBs can be accessed with RMI or as SOAP-RESTful endpoint. I want to access a remote EJB from another computer/ip address for example in a standalone application. I can reach to EJBs with web services endpoint then i dont know to reach with RMI. How can i implement this idea. I'm using Glassfish 3.1.
Check out the How do I access a Remote EJB component from a stand-alone java client? document. Code snippets rely on EJB 2 (Home interfaces), you should lookup #Remote interfaces directly. Of course they must be available on the client side.
Example
Based on: Creating EJB3 Sessions Beans using Netbeans 6.1 and Glassfish:
jndi.properties:
java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
org.omg.CORBA.ORBInitialHost = localhost
org.omg.CORBA.ORBInitialPort = 3700
Main.java:
package testclient;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.InitialContext;
import stateless.TestEJBRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.load(new FileInputStream("jndi.properties"));
InitialContext ctx = new InitialContext(props);
TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
System.out.println(testEJB.getMessage());
} catch (NamingException nex) {
nex.printStackTrace();
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
See also
Call EJBs Deployed in GlassFish from the NetBeans Platform
Glassfish V3.x and remote standalone client