Saturday 20 August 2022

Sample JAX-RS REST API Implementation

 We can use different  tools and technologies to create and expose a REST API. In this blog I will explain how to create a simple JAX-RS REST API using JDeveloper. 

  • Open Jdeveloper and create a new ADF REST Web Application
  • Provide an application name - JaxRsSampleApp
  • Provide a package name - xxvk.svn
  • Select Next and Keep the default Project Name 
  • Select Finish
  • This will generate two projects. 
    • RESTModel
    • RESTWebservice
  • Delete the RESTModel project
  • Right click the RESTWebservice project and select new
  • Select Java Class and provide a name. - HelloService
  • Add following  @Path("/services") before the class definition // Note : Path can be anything
  • Select the warning/error next to Path and select add JaxRs package to the project. 
  • As soon as you add the JAX RS package to the project it will generate the main application Java class - GenericApplication with an extend jax.rs.core.Application
  • Delete the default method and add the following sample method to return sample plain text. 
  • Right click the project and select project properties
    • Got to Java EE Application
    • Context Root to a meaning full value. -- HelloService
  • Save changes 
  • Right click the Java service and select RUN. 
  • This will deploy the application to Integrated server and provide the sample URL
    • http://127.0.0.1:7101/xxvk-service/resources/services
  • Take the URL and paste in a browser. It will display the result. 
    package xxvk.svn;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

@Path("/services")
public class HelloService {
        @GET  // This will import the Get package
        @Produces(MediaType.TEXT_PLAIN)  // This will produce the out put in plain text format
        public String Sayhello(){
            return "Hello Welcome";
        }
}






No comments:

Post a Comment

IDCS - Identity Federation with Azure and Google (SAML IDP & Social IDP)

The setup involves Identity Cloud Service (IDCS) acting as the central identity provider, facilitating seamless authentication and authoriza...