How to deploy a REST service on glassfish under eclipse? - java

I've written a simple REST service class, and I need to deploy and run it under glassfish server, installed on my eclipse.
What steps have I to do in order to put this restlet service online and reach it from my browser?
This is the code:
import javax.ws.rs.*;
import javax.ws.rs.core.*;
#Path("/myApplication")
public class MailRestlet {
#SuppressWarnings("unused")
#Context
private UriInfo context;
/**
* Default constructor.
*/
public MyRestlet() {
// TODO Auto-generated constructor stub
}
#GET
#Produces("text/html")
public String getHtml() {
return "<html><body><h1>Hello World!!</h1>The service is online!!</body></html>";
}
}

You also need to configure the web.XML file, for example, as in http://download.oracle.com/docs/cd/E19776-01/820-4867/ggrby/index.html
There is also a way to avoid web.XML change, by extending the Application class from jaxrs...

As an alternative to configuring the end point using the web.xml file, you can extend the Application class as described here.
From in Eclipse, you can deploy on the server by:
Right clicking the project and then Run As > Run on Server.
Select your server from the list of servers you have connected to Eclipse on the screen that follows and click Next.
Ensure the project is listed under Configured and click Finish.
You can also set Eclipse to automatically publish to GlassFish when you change the code. Double click the GlassFish Server on the Server view. Expand the Publishing expandable list and select Automatically publish when resources change.

Related

How to create simple RESTful service in IntellijIDEA 2016? (JAX-RS)

My google-fu has failed me.
I have lost 2.5 hours trying to figure this out. I just want to make this simple RESTful service with get and post requests that generate hello world on get (i.e. localhost:9000/hello ) and on post prints to service console what was sent in bla variable.
I have found some simple examples
import javax.ws.rs.GET;
import javax.ws.rs.Path;
#Path("greeting")
public class Greeter {
#GET
public String sayHi() {
return "Hi!!";
}
}
But it doesn't work intellij doesnt recognize Path and GET annotations. It asks me if I want to implement them. I've tried on both NewProj->JavaEE->Restful Web Service and NewProj->Java->WebApp->WebServices.
Some sample generated code from one of them made this #webmethod annotation for which I wasn't able to find any info on the net. And the JetBrains video from 2013 looks like an overkill/a-bit-outdated?. This is really simple app, I don't need/know how to use/ Maven.
You need to download the jar-file containing the classes you have imported (from e.g. http://download.oracle.com/otndocs/jcp/jaxrs-2_0_rev_A-mrel-spec/index.html).
Open Project Structure, Go to Libraries, press the plus button, and add the jar-file to your project.

REST: run main method from a jar located inside "/web-inf/lib" of a web project

I'm using Jersey 1.x and Tomcat 8.0. I have a .jar file which is my main application that I've uploaded to "/web-inf/lib" of a Dynamic Web Project.
I can't seem to figure out how to run a class which contains a main method inside this jar, which is crucial for my web service because It creates a connection to the Derby DB and all my web service methods are using these connections.
It has to start running when Tomcat server starts.
That is most definitely not the way to do it. If you want to have something run when Tomcat starts, take a look at ServletContextListener. Basically you implement this interface:
import javax.servlet.ServletContextListener;
public class MyContextListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
// your init code goes here
}
}
However, I question that even this is what you want to do. If you're using a local or embedded Derby instance then you're probably ok but now you'll have to distribute the connection information some how. Traditionally in an application like you have you'll use something like DBCP (included with Tomcat - take a look at the Tomcat DataSource docs) and get the connection to the DB every time you need it. If you are just initializing the DB then ServletContextListener will allow that. But after that, don't use the ServletContextListener to also hand out database connections.

Add web services functionality to current working java application

I'm trying to create web services functionality linked to my current working java application and struggling to do it. Any ides ?
At line:
TopUpApp application = new TopUpApp();
I'm creating a new instance from the other class , but it doesn't seem to work when I test web service. Giving false as a return where it should be true.
Below is the code for web service.
package org.me;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
/**
*
* #author KS
*/
#WebService(serviceName = "DOAws")
#Stateless()
public class DOAws {
/**
* Web service operation
*/
#WebMethod(operationName = "login")
public boolean login(#WebParam(name = "username") int username, #WebParam(name = "password") String password) {
TopUpApp application = new TopUpApp();
return application.authenticate(username, password);
}
}
It works fine and I get true as a response when I run it in new main class this way (not a web service):
public class NewClass {
public static void main(String []args){
TopUpApp application = new TopUpApp();
System.out.println(application.authenticate(12345, "12345"));
}
}
To deploy your application as a web service, you need to do a little bit more than what is necessary for a normal java application.
I short, what you need to do is to deploy your application on a web server that is also able to deploy java web applications. Many such web servers exists, a couple of popular alternatives are Tomcat and Jetty.
The normal thing to do is to install the web server on the machine that is supposed to handle the requests. Next you need to deploy your application on that server. To do that, you will need to do a couple of things:
Package your application as a war (web archive). How to do this is quite straightforward, but involves a few steps. You should be able to figure this out by yourself using google.
Deploy your application to the web server. This is usually done by copying the war file to the web applications directory of your web server. E.g. the webapps directory under the tomcat installation directory.
When this is done, you just need to make sure that your web server is up and running, and you should be able to access your web services.

Java bean server not running properly

I am currently using java beans to create an application with a remote interface, i have very simple methods like printDetail which returns "abcd", just a test method. I have a session bean like this:
#Stateless
public class MySession implements MySessionRemote {
#Override
public void businessMethod() {
System.out.println("aaaababa");
}
when i deploy the server, i get this error:
SEVERE: CORE10012: Application previously deployed is not at its
original location any more
I use Glassfish 3.1
I Found the solution, go to your browser type localhost:4848, select applications in left list, disable any other running apps that was deployed. e.g mine was saying something like
SEVERE: CORE10012: Application previously deployed is not at its original location any more "C:user/ .... / XYZ "
Then you should disable the app XYZ because apparently the server is still running this app even if you have created a new one.

Add a web service to a already available Java project

I'm new to Java. I have a Java project. It runs perfectly on my Windows 7 machine. I want to use some of the functionalities of this project as Web Services to be able to use them in my Silverlight app. Both the Silverlight app and this Java project would be on the single server machine. The problem I have is that when I right click on the project there's no Web Service in the New menu. What should I do to add a web service to my project? Thanks.
Based on the article I linked in the comments above :: http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/index.html
With the JWS annotations you can setup a Web Service in your java application to expose some of its functionality. There is no extra libraries needed. The below examples were written with Java 6.
An example of Defining your web service :
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class MyWebService {
#WebMethod
public String myMethod(){
return "Hello World";
}
}
Note the 2 annotations of #WebService and #WebMethod. Read their API's which are linked and configure them as required. This example will work without changing a thing
You then only need to setup the Listener. You will find that in the class javax.xml.ws.Endpoint
import javax.xml.ws.Endpoint;
public class Driver {
public static void main(String[] args) {
String address = "http://127.0.0.1:8023/_WebServiceDemo";
Endpoint.publish(address, new MyWebService());
System.out.println("Listening: " + address);
}
}
Run this program and you will be able to hit your web service using http://127.0.0.1:8023/_WebServiceDemo?WSDL. At this point it is easy to configure what you want to send back and forth between the applications.
As you can see there is no need to setup a special web service project for your use.

Categories

Resources