TIL
항해99_TIL220612 (미니프로젝트 6일차)
Hyeongjun_Ham
2022. 6. 15. 21:31
오늘의 이슈
- - Get, Post요청은 잘 받아들이는데, Put, Delete 요청을 받아들이지 않았다.
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.exposedHeaders("Authorization")
.allowedOriginPatterns("*")
.allowedMethods("GET","POST","PUT","DELETE");
}
}
처음에는 .allowedMethods()가 없었는데 미설정 시 GET, POST만 가능하다고 한다.
실제로 셋팅해주니 정상 작동 확인했다.
출처 : https://kimyhcj.tistory.com/263
해결 방법 - No 'Access-Control-Allow-Origin' header is present on the requested resource.
JavaScript 엔진 표준 스펙에 있는 동일 출처 정책(Same-Origin Policy)이라는 보안규칙으로 인해 발생한다. (JavaScript 로 다른 웹페이지에 접근할 때는 같은 출처 - 프로토콜, 호스트명, 포트 - 의 페이지
kimyhcj.tistory.com
- 게시글 작성시에 이미지를 MultipartFile로 받아들이는데,수정 때는 파일 이름을 보여주기위해 이름만 보여주는 상황, 이미지를 바꾸지 않고 수정 버튼을 누르면 이미지가 없다고 하면서 에러발생
@PutMapping("/api/post/{postid}")
public void ModifyPost(@PathVariable Long postid,
@RequestPart(value = "img", required = false) MultipartFile multipartFile,
@RequestParam("happypoint") int happypoint,
@RequestParam("content") String content, @AuthenticationPrincipal UserDetailsImpl userDetails) {
PostDto dto = new PostDto(happypoint, content);
if (multipartFile==null) {
service.modifyPost(postid,dto,userDetails);
}else {
service.modifyPost(postid, dto, multipartFile, userDetails);
}
}
이미지의 required =false를 추가하고, 안 들어왔을 때는 이미지를 제외한 것만 수정되게 코드를 바꿨다.
잘한건지 모르겠다. 일단 잘 돌아간다.
- 오늘 나온 문제는 아니지만 랭크부분에 랭크에 중복없는 유저가 들어가야 하는데,
유저가 게시글을 달 때마다 랭크에 중복으로 같은 유저가 생성되었다.
public int checkMyRank(User user) {
Long targetId = user.getId();
User target = userRepository.findById(targetId).orElse(null);
//게시글 작성 안했을 때 꼴지로 보냄
if (target.getPosts().size() == 0) {
return userRepository.findAll().size();
}
//타겟의 게시글 수 , 해피포인트
int targetCountPost = target.getPosts().size();
int targetHappyPoint = target.getHappypoint();
int targetAvePoint = targetHappyPoint / targetCountPost;
//게시글이 있는 전체 유저리스트
List<User> userList = userRepository.findDistinctAllByPostsIsNotNull();
마지막줄에 Distinct를 추가하여 오류를 해결 했다.
정확한 이유는 모르겠지만.. SQL과 JPQL이라는 근본적 차이라고 한다.
출처 : https://os94.tistory.com/201
JPA의 사실과 오해 (사실은 JPQL과 Fetch Join의 오해)
JPA의 사실과 오해 (사실은 JPQL과 Fetch Join의 오해) 20.05.25 JPA 스터디 하며 깨닫게 된 오해 JPA를 꽤나 오래 써왔는데(그래봤자 1년이긴 하지만), 이번에 스터디를 하며 크게 잘못 알고 있던 개념 몇
os94.tistory.com