gpt4 book ai didi

java - 如何在tomcat上启动rest-spring应用程序?

转载 作者:行者123 更新时间:2023-12-02 01:56:58 24 4
gpt4 key购买 nike

我尝试使用其余 Controller 在 Spring 上创建应用程序。项目结构是 enter image description here

applcationContext.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">

<context:annotation-config/>
</beans>

web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:WEB-INF/applicationContext.xml</param-value>
</context-param>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

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

RestController 是

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

@RestController
public class TestController {
@RequestMapping(value="/test", method= RequestMethod.GET)
public void getTest(){
int i = 1;
}
}

所以,我在apache-tomcat-8.5.29上部署了这个应用程序,但是当尝试调用rest时出现404错误..

我尝试的网址是 localhost:8080/test 和 localhost:8080/appName/test

你有什么想法吗?

最佳答案

首先你应该使用不需要外部应用程序或Web服务器的springboot,而不是裸露的spring应用程序,成功运行你的springboot应用程序后,你可以搜索如何将springboot应用程序转换为能够在tomcat上运行。

这里有一些链接:

https://spring.io/guides/gs/rest-service/

https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

关于java - 如何在tomcat上启动rest-spring应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184791/

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