gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:39:22 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com