본문 바로가기
spring boot

Spring boot 다른 포트 설정

by improve 2024. 3. 11.

인텔리 제이 에서 기본으로 사용 하는 포트는 8080 이다.

하지만 다른 포트도 사용 가능하다.

 

인텔리 제이에서 RUN -> Edit Configurations에 들어간다.

이렇게 들어가게된다.

 

Modify options에 VM options 에  -Dserver.port=8081 을 넣게 되면 8081 포트로 실행 하게 된다.

 

 

●Conf.Corsfig 

package com.mh.restapi03.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


// 8080 포트에서 8081에 데이터를 불러올 때 사용한다.

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}

 

이렇게 모두 허용을 하게 되면  다른 컴퓨터에서 8081포트로 불러올 수 있다.

 

 

static 으로 aa.html 을 만들어 준다.

 

●aa.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>title</title>
    <script>
        fetch("http://localhost:8081/users")
            .then(res => {
                return res.json();
                }
            )
            .then(data => {
                console.log(data);
                }
            )
    </script>
</head>
<body>

</body>
</html>

이렇게 하면 aa.html을 다른 포트로 웹에서 불러 올 수 있다.