Spring frameworks provides the Restful web services implementation, here is one simple example to implement Web services using Spring.
http://localhost:8080/state/KL
Softwares Used
- Java 1.8
- Spring 4
- Jetty Server
Create a maven project using web-app archetype and add below dependencies
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
Create a Controller class to define our service<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
package com.vinod.vinod_spring_test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/state")
public class StateController {
@RequestMapping(value = "/{code}", method = RequestMethod.GET)
public @ResponseBody String getState(@PathVariable String code) {
String result;
if (code.equals("KL")) {
result = "Kerala";
} else {
result = "Default State";
}
return result;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/state")
public class StateController {
@RequestMapping(value = "/{code}", method = RequestMethod.GET)
public @ResponseBody String getState(@PathVariable String code) {
String result;
if (code.equals("KL")) {
result = "Kerala";
} else {
result = "Default State";
}
return result;
}
}
Spring configuration class
package com.vinod.vinod_spring_test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {
"com.vinod.vinod_spring_test"})
public class MyWebServiceSpringConfig{
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {
"com.vinod.vinod_spring_test"})
public class MyWebServiceSpringConfig{
}
Main class to start Jetty server
package com.vinod.vinod_spring_test;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class JettyWebserviceStart {
public static void main(String[] args) {
final AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(MyWebServiceSpringConfig.class);
final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext));
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
final Server server = new Server(8080);
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
server.destroy();
e.printStackTrace();
}
}
}
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class JettyWebserviceStart {
public static void main(String[] args) {
final AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(MyWebServiceSpringConfig.class);
final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext));
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
final Server server = new Server(8080);
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
server.destroy();
e.printStackTrace();
}
}
}
Output
Run the above program and hit the below urlhttp://localhost:8080/state/KL

No comments:
Post a Comment