본문 바로가기

Back-end/Spring Framework

Spring boot - Angular Project (2)

1. dao package를 생성한다.

2. 인터페이스를 생성한다.

- ProductCategoryRepository 

package com.luv2code.ecommerce.dao;

import com.luv2code.ecommerce.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.CrossOrigin;

@CrossOrigin("http://localhost:4200")
public interface ProductRepository extends JpaRepository<Product, Long> {
}

JpaRepostitory의 Product는 Entity, Long은Primary Key를 가르킨다

 

- ProductRepository 

package com.luv2code.ecommerce.dao;

import com.luv2code.ecommerce.entity.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.CrossOrigin;

@CrossOrigin("http://localhost:4200")
@RepositoryRestResource(collectionResourceRel = "productCategory", path = "product-category")
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Long> {
}

"productCategory"는 JSON entry의 name을 의미한다.

"product-category"는 /product-category 경로를 의미한다.

 

3. web 실행시키기

http://localhost:8080/api 접속해 JSON이 잘뜨는지 확인한다.

 

4. 외부에서 POST, PUT, DELETE를 사용할 수 없도록 조치 해야한다.

- 두 가지 방법이 있다. 하나는 Spring Data Rest 를 사용하지 않는것. 두번째는 그대로 사용하면서  POST, PUT, DELETE기능만 사용할 수 없도록 하는법. Spring Data Rest를 사용하면 혜택(paging, sorting etc)이 많기 때문에 포기 할 수 없다. 두번째 방법을 선택하도록 하겠다. 

 

5. config 패키지를 생성한다.

MyDataRestConfig Java파일을 생성 

package com.luv2code.ecommerce.config;

import com.luv2code.ecommerce.entity.Product;
import com.luv2code.ecommerce.entity.ProductCategory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;

@Configuration
public class MyDataRestConfig implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

        HttpMethod[] theUnsupportedActions = {HttpMethod.PUT, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PATCH};

        // disable HTTP methods for Product: PUT, POST, DELETE and PATCH
        config.getExposureConfiguration()
                .forDomainType(Product.class)
                .withItemExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions))
                .withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions));

        // disable HTTP methods for ProductCategory: PUT, POST, DELETE and PATCH
        config.getExposureConfiguration()
                .forDomainType(ProductCategory.class)
                .withItemExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions))
                .withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions));

    }
}

 

'Back-end > Spring Framework' 카테고리의 다른 글

Spring & Hibernate for Beginners 수료  (0) 2021.05.13
React - Spring Boot Toy Project (1)  (0) 2021.05.13
JDBC, JPA/Hibernate, Mybatis의 차이  (0) 2021.05.07
Spring boot - Angular Project (1)  (0) 2021.05.05
Lombok 사용하기  (0) 2021.05.05