Compare commits
1 Commits
master
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f256e7dbea |
@@ -25,7 +25,6 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'io.springfox:springfox-swagger2:2.6.1'
|
||||
implementation 'io.springfox:springfox-swagger-ui:2.6.1'
|
||||
implementation 'net.rakugakibox.util:yaml-resource-bundle:1.1'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
runtimeOnly 'mysql:mysql-connector-java'
|
||||
|
||||
@@ -4,8 +4,6 @@ import com.rest.api.advice.exception.CUserNotFoundException;
|
||||
import com.rest.api.model.response.CommonResult;
|
||||
import com.rest.api.service.ResponseService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
@@ -19,25 +17,15 @@ public class ExceptionAdvice {
|
||||
|
||||
private final ResponseService responseService;
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
protected CommonResult defaultException(HttpServletRequest request, Exception e) {
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("unKnown.code")), getMessage("unKnown.msg"));
|
||||
}
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
// protected CommonResult defaultException(HttpServletRequest request, Exception e) {
|
||||
// return responseService.getFailResult();
|
||||
// }
|
||||
|
||||
@ExceptionHandler(CUserNotFoundException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
protected CommonResult userNotFoundException(HttpServletRequest request, CUserNotFoundException e) {
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("userNotFound.code")), getMessage("userNotFound.msg"));
|
||||
}
|
||||
|
||||
private String getMessage(String code) {
|
||||
return getMessage(code, null);
|
||||
}
|
||||
|
||||
private String getMessage(String code, Object[] args) {
|
||||
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
|
||||
return responseService.getFailResult();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.rest.api.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.rakugakibox.util.YamlResourceBundle;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@Configuration
|
||||
public class MessageConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Bean // 세션에 지역설정. default는 KOREAN = 'ko'
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.KOREAN);
|
||||
return slr;
|
||||
}
|
||||
|
||||
@Bean // 지역설정을 변경하는 인터셉터. 요청시 파라미터에 lang 정보를 지정하면 언어가 변경됨.
|
||||
private LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||
lci.setParamName("lang");
|
||||
return lci;
|
||||
}
|
||||
|
||||
@Override // 인터셉터를 시스템 레지스트리에 등록
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Bean // yml 파일을 참조하는 MessageSource 선언
|
||||
public MessageSource messageSource(
|
||||
@Value("${spring.messages.basename}") String basename,
|
||||
@Value("${spring.messages.encoding}") String encoding
|
||||
) {
|
||||
YamlMessageSource ms = new YamlMessageSource();
|
||||
ms.setBasename(basename);
|
||||
ms.setDefaultEncoding(encoding);
|
||||
ms.setAlwaysUseMessageFormat(true);
|
||||
ms.setUseCodeAsDefaultMessage(true);
|
||||
ms.setFallbackToSystemLocale(true);
|
||||
return ms;
|
||||
}
|
||||
|
||||
// locale 정보에 따라 다른 yml 파일을 읽도록 처리
|
||||
private static class YamlMessageSource extends ResourceBundleMessageSource {
|
||||
@Override
|
||||
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException {
|
||||
return ResourceBundle.getBundle(basename, locale, YamlResourceBundle.Control.INSTANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,42 +30,44 @@ public class UserController {
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 단건 조회", notes = "userId로 회원을 조회한다")
|
||||
@GetMapping(value = "/user/{userId}")
|
||||
public SingleResult<User> findUserById(@ApiParam(value = "회원ID", required = true) @PathVariable int userId,
|
||||
@ApiParam(value = "언어", defaultValue = "ko") @RequestParam String lang) {
|
||||
// 결과데이터가 단일건인경우 getSingleResult를 이용해서 결과를 출력한다.
|
||||
return responseService.getSingleResult(userJpaRepo.findById(userId).orElseThrow(CUserNotFoundException::new));
|
||||
@GetMapping(value = "/user/{msrl}")
|
||||
public SingleResult<User> findUserById(@ApiParam(value = "회원ID", required = true) @PathVariable long msrl) {
|
||||
// 결과데이터가 단일건인경우 getBasicResult를 이용해서 결과를 출력한다.
|
||||
return responseService.getSingleResult(userJpaRepo.findById(msrl).orElseThrow(CUserNotFoundException::new));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 입력", notes = "회원을 입력한다")
|
||||
@PostMapping(value = "/user")
|
||||
public SingleResult<User> save(@ApiParam(value = "회원이름", required = true) @RequestParam String name,
|
||||
@ApiParam(value = "회원이메일", required = true) @RequestParam String email) {
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
user.setEmail(email);
|
||||
public SingleResult<User> save(@ApiParam(value = "회원아이디", required = true) @RequestParam String uid,
|
||||
@ApiParam(value = "회원이름", required = true) @RequestParam String name) {
|
||||
User user = User.builder()
|
||||
.uid(uid)
|
||||
.name(name)
|
||||
.build();
|
||||
return responseService.getSingleResult(userJpaRepo.save(user));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 수정", notes = "회원정보를 수정한다")
|
||||
@PutMapping(value = "/user")
|
||||
public SingleResult<User> modify(
|
||||
@ApiParam(value = "회원ID", required = true) @RequestParam int userId,
|
||||
@ApiParam(value = "회원이름", required = true) @RequestParam String name,
|
||||
@ApiParam(value = "회원이메일", required = true) @RequestParam String email) {
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setName(name);
|
||||
user.setEmail(email);
|
||||
@ApiParam(value = "회원번호", required = true) @RequestParam long msrl,
|
||||
@ApiParam(value = "회원아이디", required = true) @RequestParam String uid,
|
||||
@ApiParam(value = "회원이름", required = true) @RequestParam String name) {
|
||||
User user = User.builder()
|
||||
.msrl(msrl)
|
||||
.uid(uid)
|
||||
.name(name)
|
||||
.build();
|
||||
return responseService.getSingleResult(userJpaRepo.save(user));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 삭제", notes = "userId로 회원정보를 삭제한다")
|
||||
@DeleteMapping(value = "/user/{userId}")
|
||||
@DeleteMapping(value = "/user/{msrl}")
|
||||
public CommonResult delete(
|
||||
@ApiParam(value = "회원ID", required = true) @PathVariable int userId) {
|
||||
userJpaRepo.deleteById(userId);
|
||||
@ApiParam(value = "회원번호", required = true) @PathVariable long msrl) {
|
||||
userJpaRepo.deleteById(msrl);
|
||||
// 성공 결과 정보만 필요한경우 getSuccessResult()를 이용하여 결과를 출력한다.
|
||||
return responseService.getSuccessResult();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package com.rest.api.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
//@Table(name = "user")
|
||||
@Builder // builder를 사용할수 있게 합니다.
|
||||
@Entity // jpa entity임을 알립니다.
|
||||
@Getter // user 필드값의 getter를 자동으로 생성합니다.
|
||||
@NoArgsConstructor // 인자없는 생성자를 자동으로 생성합니다.
|
||||
@AllArgsConstructor // 인자를 모두 갖춘 생성자를 자동으로 생성합니다.
|
||||
@Table(name = "user") // 'user' 테이블과 매핑됨을 명시
|
||||
public class User {
|
||||
@Id // pk
|
||||
@Id // primaryKey임을 알립니다.
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
// pk생성전략을 DB에 위임한다는 의미입니다. mysql로 보면 pk 필드를 auto_increment로 설정해 놓은 경우로 보면 됩니다.
|
||||
private long msrl;
|
||||
@Column(nullable = false, unique = true, length = 30) // uid column을 명시합니다. 필수이고 유니크한 필드이며 길이는 30입니다.
|
||||
private String uid;
|
||||
@Column(nullable = false, length = 100) // name column을 명시합니다. 필수이고 길이는 100입니다.
|
||||
private String name;
|
||||
private String email;
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.rest.api.repo;
|
||||
import com.rest.api.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserJpaRepo extends JpaRepository<User, Integer> {
|
||||
public interface UserJpaRepo extends JpaRepository<User, Long> {
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Service // 해당 Class가 Service임을 명시합니다.
|
||||
public class ResponseService {
|
||||
|
||||
// enum으로 api 요청 결과에 대한 code, message를 정의합니다.
|
||||
@@ -52,11 +52,11 @@ public class ResponseService {
|
||||
return result;
|
||||
}
|
||||
// 실패 결과만 처리하는 메소드
|
||||
public CommonResult getFailResult(int code, String msg) {
|
||||
public CommonResult getFailResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
result.setSuccess(false);
|
||||
result.setCode(code);
|
||||
result.setMsg(msg);
|
||||
result.setCode(CommonResponse.FAIL.getCode());
|
||||
result.setMsg(CommonResponse.FAIL.getMsg());
|
||||
return result;
|
||||
}
|
||||
// 결과 모델에 api 요청 성공 데이터를 세팅해주는 메소드
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
unKnown:
|
||||
code: "-9999"
|
||||
msg: "An unknown error has occurred."
|
||||
userNotFound:
|
||||
code: "-1000"
|
||||
msg: "This member not exist"
|
||||
@@ -1,6 +0,0 @@
|
||||
unKnown:
|
||||
code: "-9999"
|
||||
msg: "알수 없는 오류가 발생하였습니다."
|
||||
userNotFound:
|
||||
code: "-1000"
|
||||
msg: "존재하지 않는 회원입니다."
|
||||
Reference in New Issue
Block a user