프로그래밍

스프링부트 restApi 샘플 코드

API class

@RequiredArgsConstructor

@RestController

@RequestMapping(“/test/command”)

public class TestCmdApi {

private final TestCmdCntr cntr;

@PostMapping(path = “saveTest”)

public TestOutCmdPyld saveTest(@RequestBody TestInCmdPyld inPayload) {

return cntr.callCntr(inPayload);

}

}

Out Payload class

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

public class TestOutCmdPyld {

private TestOutCmdDto testOutCmdDto;

}

Out Dto class

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

public class TestOutCmdDto implements Serializable  {

private String userName;

}

In Payload class

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

public class TestInCmdPyld {

private TestInCmdDto testInCmdDto;

}

In Dto class

@Data

@Builder

@AllArgsConstructor

@NoArgsConstructor

public class TestInCmdDto implements Serializable  {

private String userId;

}

Controller class

@RequiredArgsConstructor

@Component

public class TestCmdCntr {

private final TestCmdSvc svc;

@Transactional

public TestOutCmdPyld callCntr(TestInCmdPyld inPyld) {

TestOutCmdDto outDto = new TestOutCmdDto();

String id =inPyld.getTestInCmdDto().getUserId();

//TODO

outDto = Optional.ofNullable( svc.test(id) ).orElse(TestOutCmdDto.builder().build());

return TestOutCmdPyld.builder().testOutDto(outDto).build();

}

Service class

@RequiredArgsConstructor

@Service(“TEST_SVC”)

public class TestCmdSvc {

private final TestCmdRepo testRepo;

public int test(String id) {

return testCmdRepo.testSave(id);

}

CMD REPOSITORY Class

@Repository(TEST_CMD_REPO)

public interface testCmdRepo extends CrudRepository<TestCmdEntt, String>{

@Modifying

@Query(TestCmdSql.INSERT_TEST)

public int testSave(String userId);

}

SQL class

public class TestCmdSql {

public static final String INSERT_TEST = “””

INSERT INTO TEST (USER_ID)

VALUES ( :userId)

“””;

}

Query Repository class

@Repository(TEST_QRY_REPO)

public interface testQryRepo extends PagingAndSortingRepository<TestQryEntt, String>{

@Query(TestQrySql.SELECT_TEST)

String selectTest();

}

error: Content is protected !!