Compare commits
1 Commits
feature/h2
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f256e7dbea |
@@ -23,6 +23,8 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
|
||||
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'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
runtimeOnly 'mysql:mysql-connector-java'
|
||||
|
||||
31
src/main/java/com/rest/api/advice/ExceptionAdvice.java
Normal file
31
src/main/java/com/rest/api/advice/ExceptionAdvice.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.rest.api.advice;
|
||||
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestControllerAdvice
|
||||
public class ExceptionAdvice {
|
||||
|
||||
private final ResponseService responseService;
|
||||
|
||||
// @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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.rest.api.advice.exception;
|
||||
|
||||
public class CUserNotFoundException extends RuntimeException {
|
||||
public CUserNotFoundException(String msg, Throwable t) {
|
||||
super(msg, t);
|
||||
}
|
||||
|
||||
public CUserNotFoundException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public CUserNotFoundException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
30
src/main/java/com/rest/api/config/SwaggerConfiguration.java
Normal file
30
src/main/java/com/rest/api/config/SwaggerConfiguration.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.rest.api.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfiguration {
|
||||
@Bean
|
||||
public Docket swaggerApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2).apiInfo(swaggerInfo()).select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.rest.api.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build()
|
||||
.useDefaultResponseMessages(false); // 기본으로 세팅되는 200,401,403,404 메시지를 표시 하지 않음
|
||||
}
|
||||
|
||||
private ApiInfo swaggerInfo() {
|
||||
return new ApiInfoBuilder().title("Spring API Documentation")
|
||||
.description("앱 개발시 사용되는 서버 API에 대한 연동 문서입니다")
|
||||
.license("happydaddy").licenseUrl("http://daddyprogrammer.org").version("1").build();
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,73 @@
|
||||
package com.rest.api.controller.v1;
|
||||
|
||||
import com.rest.api.advice.exception.CUserNotFoundException;
|
||||
import com.rest.api.entity.User;
|
||||
import com.rest.api.model.response.CommonResult;
|
||||
import com.rest.api.model.response.ListResult;
|
||||
import com.rest.api.model.response.SingleResult;
|
||||
import com.rest.api.repo.UserJpaRepo;
|
||||
import com.rest.api.service.ResponseService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Api(tags = {"1. User"})
|
||||
@RequiredArgsConstructor
|
||||
@RestController // 결과값을 JSON으로 출력합니다.
|
||||
@RestController
|
||||
@RequestMapping(value = "/v1")
|
||||
public class UserController {
|
||||
private final UserJpaRepo userJpaRepo;
|
||||
|
||||
@GetMapping(value = "/user")
|
||||
public List<User> findAllUser() {
|
||||
return userJpaRepo.findAll();
|
||||
private final UserJpaRepo userJpaRepo;
|
||||
private final ResponseService responseService; // 결과를 처리할 Service
|
||||
|
||||
@ApiOperation(value = "회원 리스트 조회", notes = "모든 회원을 조회한다")
|
||||
@GetMapping(value = "/users")
|
||||
public ListResult<User> findAllUser() {
|
||||
// 결과데이터가 여러건인경우 getListResult를 이용해서 결과를 출력한다.
|
||||
return responseService.getListResult(userJpaRepo.findAll());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 단건 조회", notes = "userId로 회원을 조회한다")
|
||||
@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 User save() {
|
||||
public SingleResult<User> save(@ApiParam(value = "회원아이디", required = true) @RequestParam String uid,
|
||||
@ApiParam(value = "회원이름", required = true) @RequestParam String name) {
|
||||
User user = User.builder()
|
||||
.uid("yumi@naver.com")
|
||||
.name("유미")
|
||||
.uid(uid)
|
||||
.name(name)
|
||||
.build();
|
||||
return userJpaRepo.save(user);
|
||||
return responseService.getSingleResult(userJpaRepo.save(user));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "회원 수정", notes = "회원정보를 수정한다")
|
||||
@PutMapping(value = "/user")
|
||||
public SingleResult<User> modify(
|
||||
@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/{msrl}")
|
||||
public CommonResult delete(
|
||||
@ApiParam(value = "회원번호", required = true) @PathVariable long msrl) {
|
||||
userJpaRepo.deleteById(msrl);
|
||||
// 성공 결과 정보만 필요한경우 getSuccessResult()를 이용하여 결과를 출력한다.
|
||||
return responseService.getSuccessResult();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.rest.api.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@@ -22,4 +19,4 @@ public class User {
|
||||
private String uid;
|
||||
@Column(nullable = false, length = 100) // name column을 명시합니다. 필수이고 길이는 100입니다.
|
||||
private String name;
|
||||
}
|
||||
}
|
||||
|
||||
20
src/main/java/com/rest/api/model/response/CommonResult.java
Normal file
20
src/main/java/com/rest/api/model/response/CommonResult.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.rest.api.model.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CommonResult {
|
||||
|
||||
@ApiModelProperty(value = "응답 성공여부 : true/false")
|
||||
private boolean success;
|
||||
|
||||
@ApiModelProperty(value = "응답 코드 번호 : > 0 정상, < 0 비정상")
|
||||
private int code;
|
||||
|
||||
@ApiModelProperty(value = "응답 메시지")
|
||||
private String msg;
|
||||
}
|
||||
|
||||
12
src/main/java/com/rest/api/model/response/ListResult.java
Normal file
12
src/main/java/com/rest/api/model/response/ListResult.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.rest.api.model.response;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ListResult<T> extends CommonResult {
|
||||
private List<T> list;
|
||||
}
|
||||
10
src/main/java/com/rest/api/model/response/SingleResult.java
Normal file
10
src/main/java/com/rest/api/model/response/SingleResult.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.rest.api.model.response;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class SingleResult<T> extends CommonResult {
|
||||
private T data;
|
||||
}
|
||||
68
src/main/java/com/rest/api/service/ResponseService.java
Normal file
68
src/main/java/com/rest/api/service/ResponseService.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.rest.api.service;
|
||||
|
||||
import com.rest.api.model.response.CommonResult;
|
||||
import com.rest.api.model.response.ListResult;
|
||||
import com.rest.api.model.response.SingleResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service // 해당 Class가 Service임을 명시합니다.
|
||||
public class ResponseService {
|
||||
|
||||
// enum으로 api 요청 결과에 대한 code, message를 정의합니다.
|
||||
public enum CommonResponse {
|
||||
SUCCESS(0, "성공하였습니디."),
|
||||
FAIL(-1, "실패하였습니다.");
|
||||
|
||||
int code;
|
||||
String msg;
|
||||
|
||||
CommonResponse(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
// 단일건 결과를 처리하는 메소드
|
||||
public <T> SingleResult<T> getSingleResult(T data) {
|
||||
SingleResult<T> result = new SingleResult<>();
|
||||
result.setData(data);
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 다중건 결과를 처리하는 메소드
|
||||
public <T> ListResult<T> getListResult(List<T> list) {
|
||||
ListResult<T> result = new ListResult<>();
|
||||
result.setList(list);
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 성공 결과만 처리하는 메소드
|
||||
public CommonResult getSuccessResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 실패 결과만 처리하는 메소드
|
||||
public CommonResult getFailResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
result.setSuccess(false);
|
||||
result.setCode(CommonResponse.FAIL.getCode());
|
||||
result.setMsg(CommonResponse.FAIL.getMsg());
|
||||
return result;
|
||||
}
|
||||
// 결과 모델에 api 요청 성공 데이터를 세팅해주는 메소드
|
||||
private void setSuccessResult(CommonResult result) {
|
||||
result.setSuccess(true);
|
||||
result.setCode(CommonResponse.SUCCESS.getCode());
|
||||
result.setMsg(CommonResponse.SUCCESS.getMsg());
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,8 @@ spring:
|
||||
username: sa
|
||||
jpa:
|
||||
database-platform: org.hibernate.dialect.H2Dialect
|
||||
properties.hibernate.hbm2ddl.auto: update
|
||||
showSql: true
|
||||
#properties.hibernate.hbm2ddl.auto: none
|
||||
showSql: true
|
||||
messages:
|
||||
basename: i18n/exception
|
||||
encoding: UTF-8
|
||||
Reference in New Issue
Block a user