DBILITY

독거 가능성 100% 노후에 라면값이라도 하게 센스를 발휘합시다!😅
Please click on the ad so that I can pay for ramen in my old age!
点击一下广告,让老后吃个泡面钱吧!
老後にラーメン代だけでもするように広告を一回クリックしてください。

spring boot rest api cors 본문

java/spring boot

spring boot rest api cors

DBILITY 2022. 2. 17. 16:00
반응형

간단하게 허용하고 싶은 request mapping에 annotation으로 처리해 보았다.

개발 중인 react app가 3000번 포트에서 실행 중이고, springboot rest api는 9090으로 운영 중에 요청 처리를 한 것이다.

서버 쪽에서 허용을 해주는 것이므로, 요청하는 app의 경로를 적어 주면 되나 보다.

package com.dbility.apps.dev.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class RoomsController {

    @Resource(name="roomsService")
    private RoomsService roomsService;

    @CrossOrigin(origins = "http://localhost:3000")
    @GetMapping(value = "/findall")
    public List<RoomsEntity> getRooms() throws Exception {
        return roomsService.findAll();
    }

}

전체에 대한 설정은 appconfig에서 하면 되겠지?

다음과 같이 해봤다.

package com.dbility.apps.dev.test;

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

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET","POST")
                .allowedOrigins("http://localhost:3000")
                .allowCredentials(true);
    }
}
반응형

'java > spring boot' 카테고리의 다른 글

spring boot embedded tomcat docroot change  (0) 2022.02.24
maven Blocked mirror for repositories~  (0) 2022.02.19
spring data jpa study code  (0) 2022.02.18
Comments