English 中文(简体)
利用春天中心提供的简单教育、科学和技术服务,组合问题
原标题:Simple REST service using Spring MVC, problems with configuration
  • 时间:2011-11-10 20:30:57
  •  标签:
  • java
  • spring

I m 试图利用春天工具箱和Eclipse创建的M.VC模板项目增加一个简单的REE服务。 我所要揭露的科技创新服务基本上是一种与任何模式无关的用途。 这些投入是一纸空文,我希望它回过来。 守则部分比较容易,但配置并非易事。 以下是我的相关档案。

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

网络:

<?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>

休息服务。

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;
    }
}

事先得到任何帮助!

- 共同

最佳回答

First off, you reversal function is incorrect

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

它从来不会,因为你期望<代码>i在开始时低于或等于0

Second of all, here s some good news

“entergraph

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( "
	 [" + id + "] reversed ==> " + reversed.toString() );

        model.addAttribute( "originalId", id );
        model.addAttribute( "reversedId", reversed.toString() );

        return "home";
    }
}

主计长姓名为HomeController,因为我使用了一个模板,即:你可以在两点点点上创建:

  • 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!"

但是,其方式已经消失了,从上述法典中消失了,看到了决心和<条码>。

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

为了恢复正义,仅需要做两件事:

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:

“entergraph

aReversalResult有两个领域:reverseString/code>的静态方法,并在构造中推翻了强硬:

    private String original;
    private String reversed;

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

当然,这只是一个单独的职能,但我要再次表明目标,把1+领域回来。

问题回答

@tolitius是正确的,但还有一个问题是,你没有告诉大家,什么是想使用的。 如果@RequestMapping 方法回归String,春天希望“努力”成为一种观点的名称。 如果您希望返回数值达到be<>。 答复后,你又将答复留给春天来决定与答复有关的内容。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...