3

I'm trying to rename my Spring MVC web application. When I run it, there is an old name in the URL: http://localhost:8080/oldName/

In project Properties>Resource I set Path: /newName and also in Web Project Settings, Context root: newName

But it didn't work, I still have http://localhost:8080/oldName/ How to rename it?

Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
jarosik
  • 4,136
  • 10
  • 36
  • 53

2 Answers2

0

There are more than one ways and it depends on if you are using spring-boot for example or not:

  1. In application.properties/yml file:

server.servlet.context-path=/newName

  1. Java System Property:

You can also set the context path as a Java system property before even the context is initialized:

public static void main(String[] args)
{
    System.setProperty("server.servlet.context-path", "/newName");
    SpringApplication.run(Application.class, args);
}
  1. OS Environment Variable:

Linux:

export SERVER_SERVLET_CONTEXT_PATH=/newName

Windows:

set SERVER_SERVLET_CONTEXT_PATH=/newName

The above environment variable is for Spring Boot 2.x.x, If we have 1.x.x, the variable is SERVER_CONTEXT_PATH.

  1. Command Line Arguments

We can set the properties dynamically via command line arguments as well:

java -jar app.jar --server.servlet.context-path=/newName

  1. Using Java Config

With Spring Boot 2, we can use WebServerFactoryCustomizer:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
    return factory -> factory.setContextPath("/newName");
}

With Spring Boot 1, we can create an instance of EmbeddedServletContainerCustomizer:

@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
    return container -> container.setContextPath("/newName");
}
  1. Eclipse + Maven

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <wtpversion>2.0</wtpversion> <wtpContextName>newName</wtpContextName> </configuration> </plugin>

    1. Eclipse + Gradle

    apply plugin: 'java' apply plugin: 'war' apply plugin: 'eclipse-wtp' eclipse { wtp { component { contextPath = 'newName' } } }

Following links might be helpful:

Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
0

For my case , I use ECLIPSE for developping my project.

In order to get the desired url after clicking "Run on Server" , I also need to amend the following line of server.xml under Tomcat-v9.0 as below :

<Context docBase="newName" path="/newName" reloadable="true" source="org.eclipse.jst.jee.server:newName"/></Host>

P.S. I have checked below : In project Properties>Resource I set Path: /newName and also in Web Project Settings, Context root: newName