gpt4 book ai didi

java - 无法访问 Spring Controller

转载 作者:行者123 更新时间:2023-12-02 10:36:38 25 4
gpt4 key购买 nike

我正在尝试从头开始创建一个非常基本的 Spring RESTful API 应用程序,但我无法访问 Controller 。我可以访问 JSP 文件,但不能访问 Controller 。我也尝试过使用 @RestController 进行注释,但没有成功。我在 Tomcat 8 上运行。

错误是:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. WEB.xml

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

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

调度程序-servlet.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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven />
<context:component-scan
base-package="com.controller />
<mvc:default-servlet-handler />

</beans>

我的 Controller 是

    @Controller
public class TransactionControllerImpl{
@Autowired
private TransactionService transactionService;

@RequestMapping(method = RequestMethod.GET, value="/transaction")
@ResponseBody
public String getTransactionList() {
try {
System.out.println("from controller");
return "HEllow rahul";//transactionService.getTransactionList();

}
catch (Exception e) {
return null;
}
}

最佳答案

如果您单独创建应用程序上下文,则应提供上下文参数和值作为 context.xml 文件的位置。

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

对于您的错误, Controller 无法访问,可能是由于:-

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

检查您是否编写了正确的基础包名称或尝试使用

<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

希望有帮助。

关于java - 无法访问 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245892/

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