2

I already have a maven project and try to import the spring framework to do a basic restful application. But it seems like my restful controller can not get the request.

My controller:

package org.itri.ccma.paas.service.event.webservice;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(produces = "application/json")
public class MyController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {

        return "true";
    }

}

My pom.xml

...
    <dependencies>

        <!-- Springframework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

<build>
   <finalName>PaaS</finalName>
    <plugins>
       <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/PaaS</path>
                <port>8080</port>
            </configuration>
            <dependencies>
            </dependencies>
        </plugin>
    </plugins>
</build>

...

The spring config xml "rest-servlet.xml" (put in the same folder with web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/task
                           http://www.springframework.org/schema/task/spring-task-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />

    <mvc:default-servlet-handler/>

    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config />
    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="org.itri.ccma.paas.service.event.webservice.MyController" />


</beans>

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp" version="3.1"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    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_3_1.xsd">
    <display-name>Archetype Created Web Application</display-name>

<context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/rest-servlet.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>           
    </servlet>
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

And I use the maven run with the Goals: tomcat7:run

After that. I expect I can connect the URL like http://127.0.0.1:8080/PaaS/rest/hello

However its failed. The error msg (I use Postman plug-in to test restful API) :

Postman error msg

<html>
    <head>
        <title>Apache Tomcat/7.0.47 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 404 - /PaaS/rest/hello</h1>
        <HR size="1" noshade="noshade">
            <p>
                <b>type</b> Status report
            </p>
            <p>
                <b>message</b>
                <u>/PaaS/rest/hello</u>
            </p>
            <p>
                <b>description</b>
                <u>The requested resource is not available.</u>
            </p>
            <HR size="1" noshade="noshade">
                <h3>Apache Tomcat/7.0.47</h3>
            </body>
        </html>

There is nothing special in server side log:

151105 11:17:35 INFO  (FrameworkServlet.java:488) FrameworkServlet 'rest': initialization started
151105 11:17:35 INFO  (AbstractApplicationContext.java:573) Refreshing WebApplicationContext for namespace 'rest-servlet': startup date [Thu Nov 05 11:17:35 CST 2015]; parent: Root WebApplicationContext
151105 11:17:35 INFO  (AutowiredAnnotationBeanPostProcessor.java:153) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
151105 11:17:35 INFO  (FrameworkServlet.java:507) FrameworkServlet 'rest': initialization completed in 42 ms

What am I missing here ? Any suggestion? Thanks a lot!

3
  • Iam not able to see the error u got on postman. can u paste it please Commented Nov 5, 2015 at 4:58
  • also can you try 127.0.0.1:8080/PaaS/hello Commented Nov 5, 2015 at 5:00
  • yes I paste the error msg in the Postman..thanks Commented Nov 5, 2015 at 5:08

1 Answer 1

1

In your rest-servlet.xml, change

<context:component-scan base-package="org.itri.ccma.paas.service.event.webservice.MyController" />

to

<context:component-scan base-package="org.itri.ccma.paas.service.event.webservice" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.