简体   繁体   中英

Spring MVC - run application in absolute path

I have a demo application of Spring MVC - I just follow udemy course for it.

I have created the first controller and view. All is working fine, however I have one doubt about. The pom file of application contains:

  <groupId>eu.smartgroup</groupId>
  <artifactId>spring-demo-mvc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

So when I run application on tomcat server it starts, but it is available with url: http://localhost:8080/spring_demo_mvc_war

Is there possibility to configure application so it will be available in the root path: http://localhost:8080/ (without project name after slash)?

Edit: Here is full application.yml

server:
  servlet:
    contextPath: /

HelloController.java

package eu.test.springdemo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/")
public class HelloController {

    @GetMapping("/")
    public String showPage() {
        return "main-menu";
    }
}

DemoAppConfig.java

package eu.test.springdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="eu.test.springdemo")
public class DemoAppConfig implements WebMvcConfigurer {

    // define a bean for ViewResolver

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

Add this line to your application.properties :

server.servlet.contextPath=/

I've tried it on my project and it works.

Write back is it working on your.

Check if you have a controller class that is annotated as such:

@Controller
@RequestMapping("spring_demo_mvc_war")

or

@RestController
@RequestMapping("spring_demo_mvc_war")

If that is the case, then make the string empty or remove the @RequestMapping (you can also change it to whatever else you like).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM