PROJECT #2 - RESTful

김형우·2022년 4월 7일
0

New Project

목록 보기
3/12

1. 컨트롤러 생성

  • restcontroller/CustomerRestController.java 생성
package com.example.restcontroller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

// 백엔드만 구현함. 화면구현X, vue.js 또는 react.js 연동

@RestController
@RequestMapping("/api/rest_customer")
public class CustomerRestController {

    // 고객 회원가입
    // 127.0.0.1:9090/ROOT/rest_customer/join
    // csrf를 넘겨줘야하는데 못줘서 405 오류뜸
    // SecurityConfig에서 설정해야함
    @RequestMapping(value = "/join", // 주소
            method = { RequestMethod.POST }, // method 타입
            consumes = { MediaType.ALL_VALUE }, // 받는타입
            produces = { MediaType.APPLICATION_JSON_VALUE } // 반환타입
    )
    public Map<String, Object> customerJoinPOST() {
        Map<String, Object> map = new HashMap<>();
        map.put("status", 200);
        return map;
    }
}

2. 환경설정

  • SecurityConfig.java
  • h2-console 예외처리와 같은방식
  • /api로 시작하는 주소는 모두 security 예외처리
// RESTful api 허용
http.csrf().ignoringAntMatchers("/api/**");
profile
The best

0개의 댓글