ResponseEntity created
2243
작성한 질문수 53

return ResponseEntity.created(URI.create("/Member/"+id)).build();
이렇게 코드 이동을 시켰느데 Header에 Location의 경로에는 이동하고 싶은 URI가 있지만 화면은 빈 화면을 보여줍니다. 어떻게 이동할 수 있나요??
선생님께서 작성하신대로해도 Header의 Location의 경로은 저렇게 있지만 실행하면 빈 화면만나옵니다. 원하는 메시지와 함께 이동을 원할 시 어떻게 해야 할까요?
답변 1
1
안녕하세요, 이도원입니다.
해당 요청에 대해 응답값으로 전달된 내용을 토대로, 특정 페이지로 이동하려고 한다면, Controller(서버) 쪽에서 직접 이동을 하던가, 프론트에서 처리해 줘야 합니다. 저희는 프론트에 대한 내용 없이 Controller에서만 처리하고 있기 때문에, ResponseEntity로 반환하지 말고, 특정 페이지를 다시 호출하시거나, ResponseEntity의 Header에 이동되고자 하는 URI를 명시하시면 됩니다. 강의에서 사용한 Location만으로는 해당 주소로 이동되지 않고, 단순히 보여주는 정보로만 사용된다고 보시면 됩니다. 아래 코드를 참보하셔서 작업해 보세요.
@GetMapping("/users/{id}")
public ResponseEntity<User> getStudent(@PathVariable int id) {
Optional<User> userOptional = userRepository.findById(id);
if (!userOptional.isPresent())
return ResponseEntity.notFound().build();
User user = userOptional.get();
return ResponseEntity.ok(user);
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
User savedUser = userRepository.save(user);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedUser.getId())
.toUri();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(location);
return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER);
}감사합니다.
JPA
0
63
1
jpa dependency를 추가하고 SecurityConfig클래스에서 오류가 납니다.
0
72
1
웹 브라우저 400 bad request
0
73
1
@Size는 되는데 @Past는 안 됩니다.
0
65
1
pdf 자료는 없나요?
0
76
2
locale 정보가 null 이면 무조건 messages_ko.properties이 호출 되는 문제
0
91
2
Swagger 강의, Unable to infer base url 이거 뜨시는 분들 도움되시라고
0
122
1
강의에서나온 화면 피피티
0
161
1
HelloWorldBean 관련 에러
0
189
2
Swagger API 3.x 오류..
0
223
1
java: variable message not initialized in the default constructor 에러는 어찌하면 좋을까요?
1
283
1
현재 GIT에 올리신 소스를 실행해봤습니다.
0
191
2
고양이 소리가 귀엽네요 !!
0
140
2
git에서 소스받고 실습중인데
0
120
1
post가 안되요
0
107
1
한국어 같은 경우 언어코드인 messages_ko.properties 로 생성하는게 더 좋지 않나요?
0
169
2
리턴타입으로서 EntityModel<User> 와 ResponseEntity질문
0
107
1
예외처리쪽 관련 질문있습니다.
0
161
2
엔티티가 바로 응답으로 나가도 되나요??
0
230
2
안녕하세여 Cannot invoke "co.kr.joneconsulting.resfulservice.repository.PostRepository.save(Object)" because "this.postRepository" is null
0
142
2
사용자 등록하고 나서 H2 에서 보면 신규 사용자의 password, ssn 이 null 로 되어 있습니다.
0
144
2
ApplictionContext 질문
0
196
2
롬북이 안먹히는것같아요
0
159
1
인텔리제이에서스프링부트 파일 실행하면
0
242
1





