How can I bootstrap Jersey REST service server? - java

I've been thrown into a project that is basically just a REST service an provides some functionality to web clients. However, I can't see any bootstrapping going on yet for the services - like not at all..
In particular I have to setup the file system for the server and its services. Therefore I am looking for a way to get control of the web application as the server is booting up and before it is loading the REST resources:
import javax.ws.rs.Path;
import com.sun.jersey.spi.resource.Singleton;
#Path("/")
#Singleton
public class EnrichmentResource {
// ...
}
How can I do that? I can only find such simple examples where a REST Controller gets defined but no bootstrapping examples.

There isn't a "pre-entry" part of JAX-RS per se. However, in any JEE application you can always define a WebListener:
#WebListener
public class MyListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println( "context initialized" );
}
}
The contextInitialized() method will be called before anything is called into your REST services. Remember that JAX-RS is still built on top of the servlet framework.

Related

How do I configure GraphQL servlet endpoint for graphql-java-servlet along with Micronaut Controllers?

I have got a requirement to implement a File Upload functionality using GraphQL. My current Micronaut project has already some Controller classes and graphql-java-servlet supports file upload, it needs a Servlet to be created. How do I configure the micronaut project for both Controllers and Servlet?
I have tried creating simple servlet from graphql-java-servlet in the micronaut project and keeping all my controllers as it is.
public class HelloServlet extends GraphQLHttpServlet {
#Override
protected GraphQLConfiguration getConfiguration() {
...
}
private GraphQLSchema createSchema() {
...
}
}```
When I start the Micronaut application. it only identifies controllers and I am able to invoke them using curl request. Unfortunately (#WebServlet) is not configured and not exposed.
Micronaut does not support the servlet API, so it is not possible to register a servlet.

Register Jersey REST Services at Startup of Spring Container

I need to register objects as Jersey Services at runtime.
I already tried through Spring applicationContext but it is registered as a normal bean.
Something like this...
jerseyContainer.register(new Service());
#Path("/service")
public class Service{
#GET
public Thing get(){
return new Thing();
}
}
or some way before the Spring container finishes its startup.
the spring-jersey integration relies on a servlet
com.sun.jersey.spi.spring.container.servlet.SpringServlet
defined in your web.xml
please refer to this tutorial: specifically point number 4 "Integrate Jersey with Spring"
you'll see how you can define the packages to be scanned for the resource classes to be exposed by Jersey

Deploying web service on cloud

Developed an web service , below are the steps
1) Create a Web Service Endpoint Interface..
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
#WebService
#SOAPBinding(style = Style.RPC)
public interface HelloWorld{
#WebMethod String getHelloWorldAsString(String name);
}
2. Create a Web Service Endpoint Implementation ..
import javax.jws.WebService;
//Service Implementation
#WebService(endpointInterface = "com.abc.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Create a Endpoint Publisher...
import javax.xml.ws.Endpoint;
import com.abc.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
Now I have also tested the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL “http://localhost:9999/ws/hello?wsdl” .
But My query is that as I new to the world of cloud , I want to deploy my webservice to cloud like amazon so that If I provide the wsdl to anyone in the world he can access my wsdl through his browser as my web service is deployed on cloud.
Please advise me how to achieve this..!!
This aproach will not work when you deploy your application to a real server on the cloud because you cannot execute your main method to publish the web service.
You need to configure something to publish your web service when your application starts on server.
For example, using Spring, to run an SOAP Web Service on Tomcat you need to inject your WS beans and use the SimpleJaxWsServiceExporter bean to publish it, these configurations are realized on your application-context.xml or equivalent.
In your case, take a look on this link, it is an example of how to publish an WSDL Web Service using JAX-WS RI distribution.
For tests, you can deploy your WAR application to Openshift.
Hope it helps,
Best Regards.

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.

Calling web services from your JSF code

Let's say that you have a presentation tier in JSF, and that your business tier is accessed using web services. How would you call your web services from JSF?
I was considering to have my backing beans to call the web services, but I just though I could use Ajax with JSF in order to connect to the web services. What would you choose and why? Any other choice you could recommend?
EDIT: I'm using Spring in the business tier, maybe that info may help with the suggestions.
Thanks.
I'd wrap the web service call in a service class, that is accessed via the managed bean. Thus the front-end will not know how exactly the data comes to it - via web services, or via any other means.
Let's say that you have a presentation tier in JSF, and that your business tier is accessed using web services. How would you call your web services from JSF?
The "classic" approach would be to inject a JAX-WS proxy factory class (generated from the WSDL) in a ManagedBean:
public class ItemController {
#WebServiceRef(wsdlLocation = "http://localhost:8080/CatalogService/Catalog?wsdl")
private CatalogService service;
public DataModel getItems() {
if (model==null || index != firstItem){
model=getNextItems();
}
return this.model;
}
public DataModel getNextItems() {
Catalog port = service.getCatalogPort();
model = new ListDataModel(port.getItems( firstItem,batchSize));
return model;
}
}
Sample taken from Sample Application using JAX-WS, JSF, EJB 3.0, and Java.
I would implement EJBs and expose them as web service (for language independet remote access) within the application I would access the EJBs by lookup and direct call them (for better performance). Unfortunatly you did not tell what platform you're using, so I can't be sure whether my suggestions would be feasible.

Categories

Resources