Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- AWS
- 오라클 병렬처리
- 트리 회전
- rabbitmq 에러
- redux
- react
- VUE
- 자바
- ACCESS_REFUSED
- 리덕스
- 도커빌드
- $emit()
- 자료구조
- quert
- vue.js
- paraller
- 애그리거트
- 네임드 뷰
- exiting abnormally
- REDIS
- EBS
- 리덕스 공식문서
- forNmae()
- 컴포넌트 주도
- 오라클
- .getClass()
- 리액트
- Express
- Java Reflextion API
- 커스텀 로그인
Archives
- Today
- Total
개발정리
[BUG] 415 (Unsupported Media) error 본문
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.Service.AuthService;
import com.example.demo.entity.dto.MemberDTO;
@CrossOrigin
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
private AuthService authService;
@PostMapping("/signup")
public ResponseEntity<?> signup(@RequestPart(value = "memberDTO") MemberDTO memberDTO,
@RequestPart(value = "profile") MultipartFile file) throws IOException {
HashMap<String,Object> hm=new HashMap<>();
UUID uuid=UUID.randomUUID();
file.transferTo(Paths.get("src/main/resources/static/images/"+uuid.toString()+"_"+file.getOriginalFilename()));
memberDTO.setProfile(uuid.toString()+"_"+file.getOriginalFilename());
if(authService.signup(memberDTO)) {
System.out.println("회원가입 성공!");
hm.put("register_stat",true);
}
else {
System.out.println("회원가입 실패!");
hm.put("register_stat",false);
}
return ResponseEntity.ok(hm);
}
@PostMapping("/signin")
public ResponseEntity<?> signin(@RequestBody MemberDTO memberDTO) {
String token = authService.signin(memberDTO);
HashMap<String,String> hm=new HashMap<>();
hm.put("token",token);
if(token==null) {
return ResponseEntity.badRequest().body(hm);
}
else {
return ResponseEntity.ok().header(HttpHeaders.SET_COOKIE,new HttpCookie("token",token).toString())
.body(hm);
}
}
}
<template>
<div class="container">
<div class="imageDiv" v-on:click="clickDiv">
<img src="" id="preview"/>
<input type="file" id="image" v-on:change="previewChange" style="visibility: hidden;"/>
</div>
<input type="text" placeholder="아이디" v-bind="userId"/>
<input type="password" placeholder="패스워드" v-bind="password"/>
<input type="button" value="회원가입" v-on:click="signup" />
</div>
</template>
<script>
import axios from 'axios';
export default{
data(){
return{
userId:"",
password:"",
profile:""
};
},
methods:{
clickDiv:function(){
const image=document.getElementById('image');
image.click();
},
previewChange:function(){
const preview=document.getElementById('preview');
const image=document.getElementById('image');
const reader=new FileReader();
if(image.files[0] ==null){
return;
}
reader.onloadend=()=>{
preview.src=reader.result;
}
console.log(image.files[0]);
this.$data.profile=image.files[0];
reader.readAsDataURL(image.files[0]);
},
signup:function(){
const formData =new FormData();
formData.append('profile',this.$data.profile,{
type:"image/jpeg"
});
formData.append('memberDTO',JSON.stringify({
userId:this.$data.userId,
password:this.$data.password
}));
axios.post('http://localhost:8080/auth/signup',formData,{
headers:{
"Content-Type":"multipart/form-data",
'Accept':'application/json'
}
})
.then(response =>{
console.log(response);
})
}
}
}
</script>
<style scoped>
.container{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 400px;
}
input{
box-sizing: content-box;
width: 200px;
padding: 0;
}
.imageDiv{
border: 1px solid black;
border-radius: 50%;
width: 100px;
height: 100px;
overflow: hidden;
}
.imageDiv>img{
width: 100%;
height: 100%;
}
</style>
vue.js 에서 axios로 파일과 json을 보내는데 415 (Unsupported Media) error 발생
24/02/04 해결
자바스크립트에서 JSON을 전송할 때 new Blob을 사용하여 전송 해야합니다.
signup:function(){
const formData =new FormData();
const request = {
userId:this.$data.userId,
password:this.$data.password
}
formData.append('profile',this.$data.profile);
formData.append('memberDTO',new Blob([JSON.stringify(request)],{type:'application/json'}));
console.log(this.$data.profile);
axios.post('http://localhost:8080/auth/signup',formData)
.then(response =>{
console.log(response);
})
}
'개발 > 중거거래사이트 클론' 카테고리의 다른 글
[트러블 슈팅] Cannot read properties of undefined (reading 'userId') (0) | 2024.02.23 |
---|---|
[트러블 슈팅]-라우터 params undefined 문제 (0) | 2024.02.20 |
[트러블 슈팅] jpa 양방향 매핑 조회시 stackoverflow 문제 (0) | 2024.02.18 |
[트러블 슈팅] vue.js axios 인터셉터 설정 (0) | 2024.02.16 |
[트러블 슈팅]로그인시 로그아웃으로 변경 (0) | 2024.02.05 |