java - Spring MVC Web 应用程序中未找到 Json Webservice

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

我有一个 Spring Web 应用程序,该应用程序有两个方面!一种是 Json Web 服务,另一种是静态 View 。当我启动 Web 应用程序并使用 URL 转到 View 时,它工作正常。但是当我尝试连接到 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);
    }

}

我在这里做错了什么!我已经定义了两个具有给定优先级的 View 解析器!但有些它不起作用!请帮我解决这个问题!

谢谢。

更新:

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

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 错误!

enter image description here

最佳答案

您正在将 View 映射为 jsonView <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>但稍后在 Controller 的返回方法中,您将返回 ModelAndView("jasonView", model);

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

关于java - Spring MVC Web 应用程序中未找到 Json Webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142200/

相关文章:

php - 根据URL参数生成SQL查询

java - 获取Spring WebClient请求的状态码

Java HTTP/2 服务器套接字

javascript - 从另一个 Web 服务的 Success 函数调用 C# Web 服务?

web-services - 在服务器上运行 asp.net WebAPI,无需安装 asp.net mvc 4 beta

spring - Grails或Spring恒定时间比较功能?

java - Spring - 在配置中使用工厂bean?

java - 具有 Spring Security 和 Java 配置的自定义身份验证管理器

java - 如何测试数据库连接是否成功关闭?

java - 窗口大小调整后调整组件大小