본문 바로가기

Spring실습

안드로이드스튜디오와 스프링부트의 @PUT, @PutMapping

반응형

어쩌면 오랜만이겠죠

 

이번에는 안드로이드 스튜디오와 스프링부트에서 PUT method를 구현하는 방법을 소개해보겠다.

 

put이란 update의 기능을 수행할 때 사용한다.

 

우선 android studio part이다.

 

UserAPI.kt

@PUT("admin/user/modify/{userId}")
suspend fun modifyUser(@Path("userId") userId: String, @Body userModel: UserModel): Response<Void>

 

특정 id를 인식해서 해당 id의 db값을 바꾸기 위해 userId를 입력받는다.

그 외에는 post때 구현한 코드와 거의 다를것이 없다.

 

이제부터는 spring boot part이다.

 

1. UserController.java

@PutMapping("admin/user/modify/{userId}")
public ResponseEntity<UserDTO> modifyUser(@PathVariable String userId, @RequestBody UserDTO userDTO){
    //String userId = userDTO.getUserId();
    String userPassword = userDTO.getUserPassword();
    String userName = userDTO.getUserName();
    String userEmail = userDTO.getUserEmail();
    int userAge = userDTO.getUserAge();

    UserDTO response = userService.saveUser(userId, userPassword, userName, userEmail, userAge);

    return ResponseEntity.status(HttpStatus.OK).body(response);
}

보면 post 때 구현한 code와 매우 흡사한 모습을 볼 수 있다.

그리고 심지어 controller code를 제외하고는 post 때 구현한 code와 똑같다.

 

결과

수정 btn누르면

이렇게 edtit text로 뜨는데 여기서 값을 변경하면 

변경하고 화면을 갱신하면 db에 신규 값이 저장됨을 확인할 수 있다.

 

***************** 앞으로의 계획 ****************************

spring boot의 경우 정상적인 sever로 사용이 가능할 때 까지 작성할 꺼긴한데 

정확한 개발 순서로는 

@DeleteMapping  -> test 기능 구현 -> code의 detail 로 할 꺼 같은데

솔직히 delete는 쉬워서 숨돌리기 느낌으로 빠르면 내일 쓸꺼 같고

test부터는 deep하게 들어가야되서 좀 걸릴꺼 같다.

그래도 test 공부좀 해야 detail을 찐하게 만들 수 있지 않을까? 싶다.

 

그리고 side project 글이 좀  뜸한데 (아래 링크가 마지막 글이다.)

https://pinlib.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C%EB%A1%9C-%EC%95%84%EB%91%90%EC%9D%B4%EC%99%80-%EC%84%9C%EB%B2%84%ED%86%B5%EC%8B%A0-%EC%A0%9C%EC%96%B4%ED%95%98%EA%B8%B0WIFI-D1-R1-ESP8266-%EB%AA%A8%ED%84%B0%EC%A0%9C%EC%96%B4-JSON

 

안드로이드로 아두이와 서버통신 제어하기(WIFI D1 R1, ESP8266, 모터제어, JSON)

저번에는 arduino nano33 iot를 이용해서 LED를 제어했었습니다. https://pinlib.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EC%95%B1%EC%9D%84-%ED%86%B5%ED%95%B4-%EC%95%84%EB%91%90%EC%9D%B4%EB%85%B8-led-%EC%A0%9C%EC%96%B4%E

pinlib.tistory.com

 

내가 구매했던 r1d1 보드가 analog input이었나 output pin이 1개 밖에 없어서

코드는 다 작성했는데 연결을 못하고 있다.

그래도 노가다로 test code 만들어서 개별로 돌렸을 때는 되었으니깐

금방하지 않을까 싶다가도 hardware 자체가 익숙하지 않아서 배선 최적화나 이런거 때문에 좀 걸릴듯

일단 내일 부품들은 배송올 꺼 같긴 하다.

 

반응형