DELETE +
반환타입이 String (삭제된 회원) ==> String 일 땐 한글이 깨진다 ! 고한다 근데 나는 안깨졌는디... 이상...
@DeleteMapping("/users/{username}") ===> 요렇게만 하면 한글이 깨지고
어노에 value 랑 produces 속성을 추가해서 한글깨짐 방지 넣어 줌!
그리고 objectMapper 도 사용해서 json형태로 변환!
@DeleteMapping(value="/users/{username}", produces = "application/json;charset=UTF-8")
public String removeUser(@PathVariable String username) throws JsonProcessingException {
UsersDto dto = dao.selectOne(username);
System.out.println(dao.delete(username)+"행이 삭제 되었습니다.");
//DTO를 문자열(JSON형태)로 변환
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(dto);
}
이렇게 하거나
ResponseEntity 객체를 이용해도 가능함(이거 파일 업로드 할 때 사용했던 객체임!)
@DeleteMapping("/users/{username}")
public ResponseEntity<UsersDto> removeUser(@PathVariable String username) throws JsonProcessingException {
UsersDto dto = dao.selectOne(username);
System.out.println(dao.delete(username)+"행이 삭제 되었습니다.");
//한글처리를 위한 응답해더 설정
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","text/plain;charset=UTF-8");
return ResponseEntity.ok().headers(headers).body(dto);
}
FileUpload
파일업로드
먼저 전에 SpringProj 에서 FileUpDownUtils.java를 가져와서
model 패키지 안에 둠!
FORM태그(enctype="multipart/form-data") 혹은 AJAX 나 postman으로 요청
postman으로 요청 시 에는 body탭의 form-data 선택 후
key 와 value입력
파일인 경우 key입력 시 옆에 file선택
요청형식
[POST] http://localhost:9090/restapi/files
밑에 이건 일반 String 일 때고
@PostMapping(value="/files",produces ="application/json;charset=UTF-8")
String 대신 ResponseEntity로 사용 가능 함!!
@PostMapping(value="/files")
public ResponseEntity<Map> upload(HttpServletRequest req,@RequestPart List<MultipartFile> files) throws IllegalStateException, IOException {
//1.서버 물리적 경로얻기
String path = req.getSession().getServletContext().getRealPath("/resources");
for(MultipartFile multipartFile:files) {
//2.file객체 생셩
//파일 중복시이름 변경
String renameFilename=FileUpDownUtils.getNewFileName(path,multipartFile.getOriginalFilename());
File file = new File(path+File.separator+renameFilename);
//3업로드
multipartFile.transferTo(file);
}
Map map = new HashMap();
map.put("success",true);
//한글처리를 위한 응답해더 설정
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","text/plain; charset=UTF-8");
System.out.println(FileUpDownUtils.base64Converter(path,"lion.jpg"));
return ResponseEntity.ok().headers(headers).body(map);
}
오전 내내 안되다가
파일 버전을 높여주니까 된다... 하 ..... 증말
'학원 > RESTAPI' 카테고리의 다른 글
01/03 75-1 [RESTAPI] 구글비전API(OCR)/Swagger (0) | 2023.01.03 |
---|---|
01/02 74-2 [RESTAPI] RestTemplate(+구글비전API사용) (0) | 2023.01.02 |
12/30 73-4 [RESTAPI] READ,UPDATE,DELETE (0) | 2023.01.01 |
12/30 73-3 [RESTAPI] CREATE(입력) (2) | 2023.01.01 |
12/30 73-2 [RESTAPI] 환경설정 (0) | 2022.12.31 |