【问题标题】:Json Webservice not found in Spring MVC Web App在 Spring MVC Web App 中找不到 Json Webservice
【发布时间】:2013-04-15 01:33:48
【问题描述】:

我有一个 Spring Web 应用程序,这个应用程序有两个方面!一个是 Json Web 服务,另一个是静态视图。当我启动 Web 应用程序并使用 URL 转到视图时,它工作正常。但是当我尝试连接到 Json Web 服务时,服务器返回 404 错误。

这是我的 dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--  SPECIFIC CONFIGURATIONS -->
    <import resource="springConfigurations/common-config.xml"/>
    <import resource="springConfigurations/mvc-config.xml"/>

    <bean id="exceptionResolver" class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver">
        <property name="exceptionView">
            <value>jsonView</value>
        </property>
        <property name="errorHandler">
            <list>
                <ref bean="statusError" />
                <ref bean="modelFlagError" />
            </list>
        </property>
        <property name="exceptionHandler">
            <list>
                <ref bean="exceptionMessageExceptionHandler" />
                <ref bean="stackTraceExceptionHandler" />
            </list>
        </property>
    </bean>

    <bean name="exceptionMessageExceptionHandler" class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler" />
    <bean name="stackTraceExceptionHandler" class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler"/>    

    <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError" />
    <bean name="modelFlagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError" />

</beans>

这是 common-config.xml 文件

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

        <!--  COMMON CONFIGURATIONS -->
        <mvc:annotation-driven/>
        <tx:annotation-driven/> 

        <context:component-scan base-package="com.jkcs.touchpos.application.controller" />

        <bean class="org.springframework.web.servlet.view.XmlViewResolver">
            <property name="location">
                <value>/WEB-INF/views.xml</value>
            </property>
            <property name="order" value="0" />
        </bean>

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
            <property name="order" value="1" />
        </bean>

        <!-- Annotations based Configuration -->
        <context:annotation-config />


        <!-- Components Auto-Detection (Backend) -->
        <context:component-scan base-package="com.jkcs.touchpos" use-default-filters="false" >
            <!-- Types annotated by Spring Managed, Controller and Transactional, or by an annotation that itself is 
            annotated by SpringManaged, Controller, Transactional -->
            <context:include-filter type="annotation" expression="com.jkcs.touchpos.platform.annotations.SpringManaged"/>
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.transaction.annotation.Transactional"/>
        </context:component-scan>

        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

</beans>

Views.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
    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.2.xsd">

    <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>

</beans>

服务类。

@Controller
public class TouchPosService {

    @RequestMapping(value = "/service", method = RequestMethod.GET)
    public ModelAndView sampleJsonService(){

        Map<String, String> model = new HashMap<String, String>();
        model.put("Value", "Test Service to provide touchPos Data");

        return new ModelAndView("jasonView", model);
    }

}

我在这里做错了什么!我已经定义了两个具有特定优先级的视图解析器!但有些是怎么不工作的!请帮我解决这个问题!

谢谢。

更新:

当服务器启动时,控制台会记录以下内容。

2013-04-22 13:40:51.384:INFO::Logging to StdErrLog::DEBUG=false via org.eclipse.jetty.util.log.StdErrLog
log4j:WARN No appenders could be found for logger (com.jkcs.touchpos.server.jetty.ServerRunner).
log4j:WARN Please initialize the log4j system properly.
2013-04-22 13:40:51.384:INFO::jetty-7.0.0.v20091005
2013-04-22 13:40:51.947:INFO:/:Initializing Spring FrameworkServlet 'dispatcher'
2013-04-22 13:40:53.509:INFO::Started SelectChannelConnector@0.0.0.0:9157

然后从浏览器中抛出以下 404!

【问题讨论】:

  • 您使用哪个 URL 访问它?启动过程中是否出现异常?
  • 打开调试级别的日志,看看当你得到 404 时记录了什么。它应该提供关于配置有什么问题的提示。在问题中查看如此多的 XML 很难找出问题所在。
  • 我使用localhost:9157/service 访问服务,使用localhost:9157/layout 访问布局。服务器正常启动,没有异常。
  • 您能否发布任何错误消息以及完整的控制器代码?包括进口!另外,你在开始时看到你的映射吗?应该看起来像:2013-04-22 16:01:58,282 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - 映射“{[/],[.....]
  • 我用控制台输出更新了我的问题和我得到的错误!

标签: java web-services spring web-applications spring-mvc


【解决方案1】:

您将视图映射为 jsonView &lt;bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/&gt;,但稍后在控制器的 return 方法中,您将返回 ModelAndView("jasonView", model);

这两个意思是一样的吗?我可以查看您是否在此处返回了错误的 jsp 文件名称,这会导致 404。

【讨论】:

  • 这些都是一样的! ModelAndView 只是将模型返回到前端!
  • 是的,但如果它试图返回 jasonView.jsp 并且它被命名为 jsonView.jsp 可以解释你的 404
  • @DimalChandrasiri 这可能是正确的答案。由于XmlViewResolver 没有jasonView 的条目,它会转到链中的下一个视图解析器,即InternalViewResolver,它会在/WEB-INF/jsp 文件夹中查找视图。
  • 你是对的! :) 当我返回 ModelAndView 时,它会搜索视图,因此,由于没有名为 jasonView 的视图,它会返回 404! :)
  • 酷,如果这回答了你的问题,你会介意接受这个答案,如果它没有用任何缺失的东西修改你的问题。
猜你喜欢
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2023-03-11
相关资源
最近更新 更多