This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetMapping("/api/members/{id}") | |
public ResponseEntity<Object> member(@PathVariable Long id, HttpServletResponse response) { | |
Member member = memberDao.selectById(id); | |
if (member == null) { | |
throw new MemberNotFoundException(); | |
} | |
return ResponseEntity.status(HttpStatus.OK).body(member); | |
} | |
@ExceptionHandler(MemberNotFoundException.class) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controller; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.RestControllerAdvice; | |
import spring.MemberNotFoundException; | |
@RestControllerAdvice("controller") | |
public class ApiExceptionAdvice { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.RestControllerAdvice; | |
import spring.MemberNotFoundException; | |
@RestControllerAdvice("controller") | |
public class ApiExceptionAdvice { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping("/api/members") | |
public void newMember(@RequestBody @Valid RegisterRequest regReq, Errors errors) throws IOException { | |
if (errors.hasErrors()) { | |
String errorCodes = errors | |
.getAllErrors() | |
.stream() | |
.map(error -> error.getCodes()[0]) | |
.collect(Collectors.joining(",")); | |
return ResponseEntity | |
.status(HttpStatus.BAD_REQUEST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.web.bind.MethodArgumentNotValidException; | |
@RestControllerAdvice("controller") | |
public class ApiExceptionAdvice { | |
@ExceptionHandler(MethodArgumentNotValidException.class) | |
public ResponseEntity<ErrorResponse> handleBindException(MethodArgumentNotValidException ex) { | |
String errorCodes = ex | |
.getBindingResult() | |
.getAllErrors() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config; | |
import org.apache.tomcat.jdbc.pool.DataSource; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Profile; | |
@Configuration | |
@Profile("dev") | |
public class DsDevConfig { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); | |
context.getEnvironment().setActiveProfiles("dev"); | |
context.register(MemberConfig.class, DsDevConfig.class, DsRealConfig.class); | |
context.refresh(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class MemberConfigWithProfile { | |
@Autowired | |
private DataSource dataSource; | |
@Bean | |
public MemberDao memberDao() { | |
return new MemberDao(dataSource); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@Profile("real, test") | |
public class DataSourceJndiConfig { | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@Profile("!dev") | |
public class DsDevConfig { | |
... | |
} |