Spring Boot เปลี่ยน Context Path ตามใจเรา

Chiwa Kantawong (Pea)
2 min readAug 15, 2019

--

สร้าง Project ขึ้นมาโดยใช้ Maven นะคับ

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>www.zengcode.com</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

สร้าง Application.java

package www.zengcode.com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
}

}

สร้าง Rest Controller ขึ้นมาตัวหนึ่ง MainController.java

package www.zengcode.com.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MainController {

@GetMapping(value = "hello", produces = MediaType.APPLICATION_JSON_VALUE)
public String hello() {
return "Hello Zengcode";
}
}

ถ้าไม่มีปัญหาอะไรเข้า http://localhost:8080/hello จะได้

ต่อไปเราต้องการให้ application ของเรามี context path = /zengcode สามารถทำได้หลายวิธีดังนี้

  1. ใช้ Java System Property ดังนี้
package www.zengcode.com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) throws InterruptedException {
System.setProperty("server.servlet.context-path", "/zengcode");
SpringApplication.run(Application.class, args);
}
}

เมื่อรัน application จะมี log ให้เราเห็นประมาณนี้

. ____ _ __ _ _
/\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)

2019–08–15 14:09:49.011 INFO 89575 — — [ main] www.zengcode.com.Application : Starting Application on Chiwas-MacBook-
…..
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘/zengcode’

เราก็จะเข้า http://localhost:8080/zengcode/hello ได้

2. set property ใน application.properties หรือ yml

server.servlet.context-path=/zengcode

ก็จะได้ผลลัพธ์เช่นช้อ 1 เหมือนกันนะคับ

3. ใช้ Java Config

package www.zengcode.com.controller;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MainController {

@GetMapping(value = "hello", produces = MediaType.APPLICATION_JSON_VALUE)
public String hello() {
return "Hello Zengcode";
}

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

4. และอีกวิธีคือ Command Line Arguments

$ java -jar your_app.jar --server.servlet.context-path=/zengcode

ลองเอาไปใช้กันนะคับ

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

No responses yet