简体   繁体   English

Java maven spring 框架的 Restful API 不起作用

[英]Restful API by Java maven spring framework not working

I already have a maven project and try to import the spring framework to do a basic restful application.我已经有一个maven项目,尝试导入spring框架来做一个基本的restful应用。 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我的 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) spring config xml“rest-servlet.xml”(和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我的 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我使用 maven run 来实现目标:tomcat7:run

After that.在那之后。 I expect I can connect the URL like http://127.0.0.1:8080/PaaS/rest/hello我希望我可以连接像http://127.0.0.1:8080/PaaS/rest/hello这样的 URL

However its failed.然而它失败了。 The error msg (I use Postman plug-in to test restful API) :错误消息(我使用Postman插件测试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!非常感谢!

In your rest-servlet.xml, change在您的 rest-servlet.xml 中,更改

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM