RESTFUL WEB SERVICE EXAMPLE WITH SPRING 4

Creating a RESTful web service with Spring is pretty easy as mentioned in here [1] and here [2].

Let’s make an example.

Create a new Dynamic Web Project and convert to Maven Project in Eclipse.

proexp

<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>Spring_Test6</groupId>
	<artifactId>Spring_Test6</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

	</dependencies>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 
    <servlet>
        <servlet-name>reverser</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>reverser</servlet-name>
        <url-pattern>/reverser/*</url-pattern>
    </servlet-mapping>
  
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.reverser" />

	<mvc:annotation-driven />

</beans>
package com.reverser;

public class MyString {

	private String original;
	private String reversed;

	public MyString(String org, String rev) {
		original = org;
		reversed = rev;
	}

	public String getOriginal() {
		return original;
	}

	public void setOriginal(String original) {
		this.original = original;
	}

	public String getReversed() {
		return reversed;
	}

	public void setReversed(String reversed) {
		this.reversed = reversed;
	}

}
package com.reverser;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReverserController {

	@RequestMapping(value = "/jsonbasedreverser_1/{word}", method = RequestMethod.GET)
	public @ResponseBody MyString reverseFuncWithPathVariable(@PathVariable String word) {

		return generateResult(word);

	}

	@RequestMapping(value = "/jsonbasedreverser_2", method = RequestMethod.GET)
	public MyString reverseFuncWithRequestParam(
			@RequestParam(value = "word", defaultValue = "Los Angeles") String word) {

		return generateResult(word);

	}

	@RequestMapping(value = "/jsonbasedreverser_3/{word1}", method = RequestMethod.GET)
	public @ResponseBody MyString reverseFuncWithPathVariableAndReqParam(@PathVariable String word1,
			@RequestParam(value = "word2", defaultValue = "San Francisco") String word2) {

		String combine = word1 + "#" + word2;

		return generateResult(combine);

	}

	@RequestMapping(value = "/jsonbasedreverser_4/{word1}", method = RequestMethod.GET)
	public @ResponseBody MyString reverseFuncWithPathVariableAndTwoReqParam(@PathVariable String word1,
			@RequestParam(value = "word2", defaultValue = "San Francisco") String word2,
			@RequestParam(value = "word3", defaultValue = "Texas") String word3) {

		String combine = word1 + "#" + word2 + "#" + word3;

		return generateResult(combine);

	}

	private MyString generateResult(String word) {
		StringBuilder sb = new StringBuilder();
		sb.append(word);
		MyString my = new MyString(sb.toString(), sb.reverse().toString());

		return my;
	}

}

Test:

http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_1/London

http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_2
http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_2?word=London

http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_3/Tokyo
http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_3/Tokyo?word2=London

http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_4/Tokyo
http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_4/Tokyo?word3=Paris
http://localhost:8080/Spring_Test6-0.0.1-SNAPSHOT/reverser/jsonbasedreverser_4/Tokyo?word2=London&word3=Paris

Resources:
[1] https://spring.io/guides/gs/rest-service/

[2] http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example

[3] http://stackoverflow.com/questions/12865093/spring-3-x-json-status-406-characteristics-not-acceptable-according-to-the-requ/30767738#30767738

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: