【问题标题】:Spring Resful WS issueSpring Restful WS 问题
【发布时间】:2016-03-16 14:55:16
【问题描述】:

我正在关注http://www.java2blog.com/2015/09/spring-restful-web-services-json-example.html,正如文章中建议的那样,当我将应用程序部署到 tomcat 时,我收到了警告:
警告:[SetContextPropertiesRule]{Context} 将属性 'source' 设置为 'org.eclipse.jst.j2ee.server:restordering' 没有找到匹配的属性。 (重新排序是我的应用程序上下文)
当我尝试访问我的应用程序时,会收到以下警告:
警告:在名称为“restordering”的 DispatcherServlet 中找不到具有 URI [/restordering] 的 HTTP 请求的映射
我看到一个 404 错误页面!
我已经在 tomcat 7 和 8 上对此进行了测试。我已经创建了战争并在 eclipse 之外部署并遵循所有建议的解决方案,包括更改服务器定义(发布模块上下文以分隔 XML 文件)以及删除、添加和执行所有技巧它在上面的文章和stackoverflow中有所描述,但到目前为止没有任何效果。
在我收到“这是一个警告并且...”之前,是的,这是一个警告,但我也收到 404 错误并且无法访问我的服务。
是的,我的 restordering-servlet.xml ( context:component-scan base-package="com.obp.restordering.controller" ) 中有正确的包名称。

这里要求的是我的 web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>restordering</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>restordering</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

而restordering-servlet.xml 是:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.obp.restordering.controller" />
</beans>  

我的控制器文件是这样的:

package com.obp.restordering.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.obp.restordering.bean.Country;

@RestController
public class CountryController {

    @RequestMapping(value = "/countries", method = RequestMethod.GET, headers = "Accept=application/json")
    public List<Country> getCountries() {
        List<Country> listOfCountries = new ArrayList<Country>();
        listOfCountries = createCountryList();
        return listOfCountries;
    }

    @RequestMapping(value = "/country/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
    public Country getCountryById(@PathVariable int id) {
        List<Country> listOfCountries = new ArrayList<Country>();
        listOfCountries = createCountryList();

        for (Country country : listOfCountries) {
            if (country.getId() == id)
                return country;
        }

        return null;
    }

    // Utiliy method to create country list.
    public List<Country> createCountryList() {
        Country indiaCountry = new Country(1, "India");
        Country chinaCountry = new Country(4, "China");
        Country nepalCountry = new Country(3, "Nepal");
        Country bhutanCountry = new Country(2, "Bhutan");

        List<Country> listOfCountries = new ArrayList<Country>();
        listOfCountries.add(indiaCountry);
        listOfCountries.add(chinaCountry);
        listOfCountries.add(nepalCountry);
        listOfCountries.add(bhutanCountry);
        return listOfCountries;
    }
}

有什么想法吗?

【问题讨论】:

  • 嗨...尝试发布您的 web.xml 和 spring 上下文配置,以便我们可能获得有关该问题的更多信息(如果与某些错误配置有关)

标签: eclipse spring rest maven


【解决方案1】:

这很尴尬,但我需要坦白承认并提供答案,这样如果其他人也有同样的问题,他们就不会像我一样绕着几天转。
上述教程的作者 Arpit 回复了我的帮助请求,并在检查我的代码后确认所有工作正常!
问题在于我如何尝试访问我的服务!如果我去 /restordering/countries 我得到 404 错误。要访问该服务,我需要前往 /restordering/restordering/countries!就如此容易。 我仍然收到警告,目前无法解决,但至少服务正在运行,这似乎更像是部署/设置问题,而不是实际的 Spring 开发问题。
无论如何,我认为这可能对其他人有所帮助。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多