Skip to content

Instantly share code, notes, and snippets.

View CheolhoJeon's full-sized avatar
🙏

Cheolho Jeon CheolhoJeon

🙏
View GitHub Profile
@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)
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 {
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 {
@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)
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()
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 {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");
context.register(MemberConfig.class, DsDevConfig.class, DsRealConfig.class);
context.refresh();
@Configuration
public class MemberConfigWithProfile {
@Autowired
private DataSource dataSource;
@Bean
public MemberDao memberDao() {
return new MemberDao(dataSource);
}
@Configuration
@Profile("real, test")
public class DataSourceJndiConfig {
...
}
@Configuration
@Profile("!dev")
public class DsDevConfig {
...
}