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

时间:2018-09-05 12:10:12

标签: java spring

我尝试在Spring上使用Rest-Controllers创建应用程序。 项目结构是 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

您有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

以下是一些链接:

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

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