Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:08:37+00:00 2026-05-26T18:08:37+00:00

I’m trying to add a simple REST service to a default Spring MVC Template

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:08:38+00:00Added an answer on May 26, 2026 at 6:08 pm

    First off, you reversal function is incorrect

    for (int i = id.length() - 1; i <= 0; --i) {...}
    

    It will never go in, as you expect i to be less than or equal to 0 on start.

    Second of all, here’s some good news

    enter image description here

    Here is a working controller

    @Controller
    @RequestMapping( value="/stringreverser" )
    public class HomeController {
    
        private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
        @RequestMapping( value="/{id}", method=RequestMethod.GET )
        public String reverseId( @PathVariable String id, Model model ) {
    
            StringBuilder reversed = new StringBuilder();
            for ( int i = id.length() - 1; i >= 0; i-- ) {
                reversed.append( id.charAt( i ) );
            }
    
            logger.debug( "\n\t [" + id + "] reversed ==> " + reversed.toString() );
    
            model.addAttribute( "originalId", id );
            model.addAttribute( "reversedId", reversed.toString() );
    
            return "home";
        }
    }
    

    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 Project
    • Choose “Spring MVC Project”, enter project name, top level package ( e.g. org.guru.xyz ), click Next
    • You have yourself a brand new “Spring MVC” project
    • In order to deploy it, right click on your project ( on the left hand side ), go to “Run As” => “Run On Server”
    • This will deploy it to Tomcat and open a “localhost:8080/somemvc/” where you would see “Hello world!”

    But 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.jsp

    <html>
      <head> <title>Mean ID Reverser</title> </head>
      <body>
        <h1> Mean ID Reverser </h1>
        <p>  I just reversed your ID "${originalId}" => "${reversedId}" </p>
      </body>
    </html>
    

    And

    <url-pattern>/restfultest/*</url-pattern> in web.xml works 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

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    

    Change a return type to @ResponseBody Object

    @RequestMapping( value="/{id}", method=RequestMethod.GET )
    public @ResponseBody Object reverseIdJson(@PathVariable String id) {        
        return new ReverserResult( id );
    }
    

    I included an object ReversalResult instead 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:

    enter image description here

    where a ReversalResult has two fields, a reverseString static method, and it reverses a String in a constructor:

        private String original;
        private String reversed;
    
        public ReverserResult( String reverseMe ) {
            this.original = reverseMe;
            this.reversed = reverseString( reverseMe );
        }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.