여백에 도장 찍기

HATEOAS 본문

Web Framework/Spring Boot

HATEOAS

Linzyseo 2019. 7. 14. 15:56

HATEOAS: Hypermedia As a The Engine Of Application State

 

사용방법

1.  pom.xml 에 HATEOAS 관련 dependency 추가

<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

 

2. 테스트 코드를 작성해보자. 

import org.springframework.hateoas.ResourceSupport;

public class Customer extends ResourceSupport {
    private String customerId;
    private String customerName;
    private String companyName;

    public Customer() {

    }

    public Customer(String customerId, String customerName, String companyName) {
        this.customerId = customerId;
        this.customerName = customerName;
        this.companyName = companyName;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }


}

 

 

import analysis.data.system.model.Customer;
import org.springframework.hateoas.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;


@RestController
public class ApiController {

    @GetMapping(value = "/api/customer", produces = "application/json; charset=UTF-8")
    public Resource<Customer> getCustomer() {
        Customer customer = new Customer();
        customer.setCustomerId("1234");
        customer.setCustomerName("seo");
        customer.setCompanyName("mycompany");

        Resource<Customer> customerResource = new Resource<>(customer);
        customerResource.add(linkTo(methodOn(ApiController.class).getCustomer()).withSelfRel());

        return customerResource;

    }
}

 

※ trouble shooting 

: @GetMapping() 어노테이션에 produces 의 값으로 "application/json" 주지 않으면 마샬링 에러 발생. 

 

 

3. Postman을 이용해 결과를 확인해보자.  

 

'Web Framework > Spring Boot ' 카테고리의 다른 글

SLF4J  (0) 2019.07.09
Spring boot - myBatis  (0) 2019.07.08
Spring boot - MSSQL 연동 설정  (0) 2019.07.04
Spring Security - configure method override  (0) 2019.07.03
Spring Boot - 웹 디렉터리 생성 및 설정  (0) 2019.07.03
Comments