I’m trying to add a simple REST service to a default Spring MVC Template project created using the SpringSource Tool Suite (STS) and Eclipse. The REST service I want to expose is basically a utility that’s not tied to any model. The input is a string and I want it to return the string reversed. The code part is easy, but the configuration is not. Below are my relevant files.
I don’t really understand why this doesn’t work, but when I type in the URL http://localhost:8080/projectName/restfultest/stringreverser/testString I get the error message
No mapping found for HTTP request with URI [/projectName/restfultest/stringreverser/] in DispatcherServlet with name ‘restfulServlet’
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>restfulServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/restfulServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restfulServlet</servlet-name>
<url-pattern>/restfultest/*</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.sample.pkg" />
</beans:beans>
RestfulService.java
package com.sample.pkg;
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;
@Controller
@RequestMapping(value = "/stringreverser")
public class RestfulService {
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public String reverseId(@PathVariable("id") String id) {
String reversed = "";
for (int i = id.length() - 1; i <= 0; --i) {
reversed += id.charAt(i);
}
return reversed;
}
}
Thanks in advance for any help!
-Joe
First off, you reversal function is incorrect
It will never go in, as you expect
ito be less than or equal to0on start.Second of all, here’s some good news
Here is a working controller
Controller name is
HomeController, since I used a templated Spring MVC app that you can create in two clicks with Spring Tool Suite:File=>New=>Spring Template ProjectNextBut the way, it’ll already have a, missing from your code above, view resolver and a
home.jsp, that you see rendered on the picture above.Here is the
home.jspAnd
<url-pattern>/restfultest/*</url-pattern>inweb.xmlworks just fine.JSON Woodoo
In order to return it as JJ (Just JSON), there are just two things you need to do:
Add a Jackson Mapper dependency
Change a return type to
@ResponseBody ObjectI included an object
ReversalResultinstead of a simple String in order to demonstrate a transparent Jackson Mapper magic, and to see that it is truly a JSON response that comes back:where a
ReversalResulthas two fields, areverseStringstatic method, and it reverses a String in a constructor:This of course would just be a stand alone function, but again, I wanted to show the Object with 1+ fields to come back as JSON.