REST Web Services with Java Part 5 - vulabvulab

Posted on 08 March 2017 by Srinivas Nelakuditi

REST Web Services with Java Part 5

In Part 4 you have created a maven project in Eclipse IDE.

Update your pom.xml with the following:

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vulab</groupId>
    <artifactId>VULAB-REST-WEB-SERVICE</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

Create Application.java class to boot strap Spring Framework

package com.vulab.code;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        
    }

}

 

Configure a class for Jersey Configuration (JAX-RS) as RESTConfiguration.java

package com.vulab.code;

import org.glassfish.jersey.server.ResourceConfig;

import org.springframework.stereotype.Component;

@Component
public class RESTConfiguratoin extends ResourceConfig {

    public RESTConfiguratoin() {
        register(RESTEndpoint.class);
        
    }

}

 

Create a REST endpoint RESTEndpoint.java

package com.vulab.code;

import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import org.springframework.stereotype.Component;

@Component
@Path(“/api/v1/simple”)
public class RESTEndpoint {

    @GET
    public String helloworld(@QueryParam(“input”) @NotNull String input) {
        return new StringBuilder(“Hello World ” + input).toString();
    }

}

 

Run Application.java to start your REST web service.

REST web service will start on port 8080 using Embedded Tomcat Server.

Browse to the URL:

http://localhost:8080/api/v1/simple?input=vulab

You will see the out put as:

Hello World vulab

 

You have successfully created and tested a Java REST Web Service using JAX-RS standard and Jersey Implementation.

 

 

1 Comment

One Response to “REST Web Services with Java Part 5”


Leave a Reply