2017-05-21 1 views
0

내 스프링 @RestControllerPATCH 기능을 구현해야합니다.스프링 레스트 컨트롤러 패치 구현

나는 많은 질문을 보았으며 가장 일반적인 접근법은 일반 자바 Map을 사용하여이를 수행하는 것이다. null이 허용하는 맵은 POJO에서 구현이 불가능한 것처럼 보이기 때문에 null 값 또는 결측 값으로 문제를 해결하는 데 도움이됩니다.

Map에서 기존 모델 객체로 값을 반영하는 데 도움이되는 기본 기능이 Spring에 있거나 직접 구현해야합니다 (예 : Jackson 등).

답변

1

내 구현 패치를 공유 할 수 있습니다. 어떤 점에서이 패치가 도움이 되었기를 바랍니다. 나는 6 개의 필드 (이름, 타입, 주소 필드, ID, 숫자, 우편 번호)를 가진 클라이언트를 가지고 있으며 클라이언트를 편집하고 아무 것도 변경할 수 있습니다. (이 아래 두 가지 이상의 다른 방법이없는 경우 또는 완전한 하나) 아니면 PATCH 다르게 할 예정이다

clientService 그냥 서비스도 부분적인 대답과 질문에 동화입니다 에

@RequestMapping(value = "/{id}", method = RequestMethod.PATCH , 
    produces = {"application/vnd.api+json"} ) 
    ResponseEntity<Resource<Client>> editClient(@PathVariable("id") Integer id, 
    @RequestBody Map<String, Object> updates) 

이 : 이는

@RequestMapping(value = "/{id}", method = RequestMethod.PATCH ,produces = {"application/vnd.api+json"}) 
    ResponseEntity<Resource<Client>> editClient(@PathVariable("id") Integer id,@RequestBody Client editedClientFromBrowser) { 
// the id is the ID of the client that I was editing.. 
//i can use this to retrieve the one from back end 
// editedClientFromBrowser is the changed Client Model Object 
//from the browser The only field changed is the one 
// that was changed in the browser but the rest of 
//the object is the same with the ID as well 

     logger.info("Edit Client reached"); 

    //retreive the same Client from backend and update and save it 
    // Client client = clientService.getClient(id); 
    // if (client == null) { 
    //  throw new EntityNotFoundException("Client not found - id: " + id); 
    // }else{ 
    //  change the field that is different from the 
    //editedClientFromBrowser and then saveAndFlush client 
    //} 

    //OR saveAndFlush the editedClientFromBrowser itself 
    clientService.saveAndFlush(editedClientFromBrowser); 
    return new ResponseEntity<>(HttpStatus.NO_CONTENT); 


    } 

이제 또 다른 내가 읽어 방법 (http://www.baeldung.com/http-put-patch-difference-spring) 및 시도 ClientRepository를 보유하고 e는 나에게 hashMap을 준다. 그러나 그것은 나에게 모든 분야를 제공합니다. 내가 변하지 않은 사람들조차도. 그래서, 정말 유익한가요? 진지하게 생각할 필요가 없습니다. 전체 클라이언트 객체를 되 찾는 것보다 더 가볍습니다.

내가 바꾼 하나 또는 두 개의 필드의 해시 맵만 얻으면 좋겠다. 그건 내가 생각하기에 패치에 더 가까웠을거야. 두 가지 구현을 어떻게 든 개선 할 수 있습니까?

관련 문제