묻고 답해요
133만명의 커뮤니티!! 함께 토론해봐요.
인프런 TOP Writers
-
미해결자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]
8강 재질문입니다 (코드포함) (해결)
코드를 다시 찬찬히 살펴봐도 틀린게 뭔지 모르겠어요...시간나시면 검토 가능 하실까요? 아니면 처음부터 다시 코딩을 해봐야겠네요////package com.group.libraryapp.UserController; import com.group.libraryapp.domain.user.User; import com.group.libraryapp.dto.calculator.request.user.response.UserResponse; import com.group.libraryapp.user.request.UserCreateRequest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.*; @RestController public class UserController { private final List<User> users = new ArrayList<>(); @PostMapping("/user") // POST /user public void saveUser(@RequestBody UserCreateRequest request){ users.add(new User(request.getName(), request.getAge())); } @GetMapping("/user") public List<UserResponse> getUsers(){ List<UserResponse> responses = new ArrayList<>(); for(int i=0; i<users.size(); i++){ responses.add(new UserResponse(i + 1, users.get(i))); }//users에 저장한 유져들을 for문으로 userResponse형태로 바꾸어 결과 list에 추가 return responses; } } package com.group.libraryapp.dto.calculator.request.user.response; import com.group.libraryapp.domain.user.User; public class UserResponse { private long id; private String name; private Integer age; public UserResponse(long id, User user) { this.id = id; this.name = user.getName(); this.age = user.getAge(); } public long getId() { return id; } public String getName() { return name; } public Integer getAge() { return age; } } package com.group.libraryapp.domain.user; public class User { private String name; private Integer age; public String getName() { return name; } public Integer getAge() { return age; } public User(String name, Integer age) { if(name == null || name.isBlank()){ throw new IllegalArgumentException(String.format("잘못된 name(%s)이 들어왔습니다.", name)); } this.name = name; this.age = age; } } // 해결했습니다.강사님께서 화면공유 방법으로 해결해주셨고 말씀해주신 해결방법은인텔리제이에서 run 후 오류내역에서 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.group.libraryapp.user.request.UserCreateRequest` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 2]확인하셨고UserCreateRequest클래스에서 기본생성자를 추가하여 해결하였습니다.매개변수가있는 생성자를 사용하면 추가로 기본생성자를 만드는 습관을 들이겠습니당
-
미해결Java/Spring 테스트를 추가하고 싶은 개발자들의 오답노트
혹시 git 플러그인 아시는 분
수업에서 git 커밋메세지 사용할 때 옆에 나오는 것들 어떻게 나오는지 아시는 분 계시나요?intellij 기능인지 플러그인인지 인 것 같은데요.아시는 분 있으시면 답변 주시면 감사하겠습니다.
-
미해결실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
실무에서 주로 사용하는 DB툴이 궁금합니다.
학습하는 분들께 도움이 되고, 더 좋은 답변을 드릴 수 있도록 질문전에 다음을 꼭 확인해주세요.1. 강의 내용과 관련된 질문을 남겨주세요.2. 인프런의 질문 게시판과 자주 하는 질문(링크)을 먼저 확인해주세요.(자주 하는 질문 링크: https://bit.ly/3fX6ygx)3. 질문 잘하기 메뉴얼(링크)을 먼저 읽어주세요.(질문 잘하기 메뉴얼 링크: https://bit.ly/2UfeqCG)질문 시에는 위 내용은 삭제하고 다음 내용을 남겨주세요.=========================================[질문 템플릿]1. 강의 내용과 관련된 질문인가요? (예/아니오)2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오)3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)[질문 내용]강의에서는 주로 H2 데이터베이스를 활용해 강의를 진행해주시는데, 실무에서는 데이터베이스를 다룰 때 보통 어떤 툴을 사용하는 지 궁금합니다.
-
미해결호돌맨의 요절복통 개발쇼 (SpringBoot, Vue.JS, AWS)
Post에 edit 메서드 삼항연산자 질문
업데이트 관련 질문입니다!!PostEdit을 만들고 Post에 edit이라는 메서드를 만들어서 PostEdit자체를 받은 후에 삼항연산자를 쓰는 방식으로 해결해도 문제가 없을까요??@Data public class PostEdit { @NotBlank(message = "제목을 입력하세요.") private String title; @NotBlank(message = "내용을 입력하세요.") private String content; @Builder public PostEdit(String title, String content) { this.title = title; this.content = content; } }public void edit(PostEdit postEdit) { this.title = postEdit.getTitle() != null ? postEdit.getTitle() : this.title; this.content = postEdit.getContent() != null ? postEdit.getContent() : this.content; }
-
미해결자바 ORM 표준 JPA 프로그래밍 - 기본편
벌크연산관련
학습하는 분들께 도움이 되고, 더 좋은 답변을 드릴 수 있도록 질문전에 다음을 꼭 확인해주세요.1. 강의 내용과 관련된 질문을 남겨주세요.2. 인프런의 질문 게시판과 자주 하는 질문(링크)을 먼저 확인해주세요.(자주 하는 질문 링크: https://bit.ly/3fX6ygx)3. 질문 잘하기 메뉴얼(링크)을 먼저 읽어주세요.(질문 잘하기 메뉴얼 링크: https://bit.ly/2UfeqCG)질문 시에는 위 내용은 삭제하고 다음 내용을 남겨주세요.=========================================[질문 템플릿]1. 강의 내용과 관련된 질문인가요? 예2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? 예3. 질문 잘하기 메뉴얼을 읽어보셨나요? 예[질문 내용]안녕하세요?벌크연산에서 정의가 헷갈려서 질문드립니다.[1] pk를 지정한뒤 jpql query문에서 insert, update, delete를 하면 이것은 벌크연산이 아니고 영속성컨텍스트의 영향을 받고, [2] pk를 지정하지 않고 한꺼번에 insert(hibernate),update,delete을 하게 되면 영속성컨텍스트와 상관없이 db와 상호작용하며 이것을 벌크연산이라고 한다.[1],[2]가 제가 제대로 이해하고 있는것이 맞을까요?
-
해결됨실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화
강의 내용은 아니지만 진도와 체득에서 질문이 있습니다..
영한 선생님 강의를.. Http부터 시작해서 지금 실전 2편까지 쭉 로드맵대로 왔는데요.. 실전 2편부터는 지금까지 MVC와는 다르게 API개념이 익숙지 않다보니 머리가 많이 뒤죽박죽이고 과열(?) 된 상태입니다..영한쌤이야 워낙 쉽게 설명해주시고 이해도 잘 되서 강의 들을 땐 좋지만 막상 누가 강의 후에 강의 내용을 기반으로 하되 강의 소스코드 보지말고 복붙하지 말고 새로운 코드를 짜봐! 하면 사실 자신 없다라는게 제 솔직한 심정인데.. 여기서 어떻게 방향을 잡아야 할지 모르겠네요.. 시간이 걸리더라도 완강한 강의 중 부족한 부분을 다시 듣고 토이프로젝트 반복 후에 실전 2편과 나머지 querydsl이나 data jpa를 들어가는게 나을지 아니면 일단 쭉 로드맵 끝낸 후에 복습으로 들어가는게 나을지 고민이 됩니다.물론 시간 충분한 학부생이면 모르겠지만 취준생 입장이라 고민이 더더욱 크네요..
-
해결됨스프링 DB 2편 - 데이터 접근 활용 기술
@Transactional 롤백,,?
안녕하세요 강사님. 프로젝트 진행중에 간단한 테스트 작업 중 이상한 결과가 나와서 질문드립니다..!@Transactional @SpringBootTest @ActiveProfiles("test") class ProductListResponseDtoTest { @Autowired ProductRepository productRepository; @Test @DisplayName("주문 상세가 주어졌을 때 ProductListResponseDto 변환") public void ofWithOrderDetail() { Product product = Product.builder() .price(1000L) .name("빵빵이") .productNo("123") .build(); productRepository.save(product); OrderDetail orderDetail = OrderDetail.builder() .product(product) .price(product.getPrice()) .quantity(2L) .build(); // when ProductListResponseDto result = ProductListResponseDto.of(orderDetail); // then assertThat(result).extracting("productId", "productNo", "name", "price", "quantity") .contains(1L, "123", "빵빵이", 1000L, 2L); } @Test @DisplayName("상품과 수량이 주어졌을 때 ProductListResponseDto 변환") public void ofWithProductAndQuantity() { Long quantity = 2L; Product product = Product.builder() .price(1000L) .name("빵빵이") .productNo("123") .build(); productRepository.save(product); // when ProductListResponseDto result = ProductListResponseDto.of(product, quantity); // then assertThat(result).extracting("productId", "productNo", "name", "price", "quantity") .contains(1L, "123", "빵빵이", 1000L, 2L); } }@Transactional를 통해 각 테스트가 롤백되어 productId가 모두 1L 될 것으로 예상하였습니다.그런데 기대와 달리 실패를 하였는데요. 첫번째 테스트(ofWithOrderDetail)의 productId의 값이 2L 되었습니다. insert문과 에러 메세지입니다.Hibernate: insert into product (category_id, created_at, deleted_at, discount_rate, is_own, is_subs, name, price, product_no, stock, thumb_img, updated_at, product_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, default) 2024-01-29T22:28:07.219+09:00 INFO 3636 --- [ main] p6spy : #1706534887219 | took 4ms | statement | connection 3| url jdbc:h2:mem:~/Marketbridge insert into product (category_id,created_at,deleted_at,discount_rate,is_own,is_subs,name,price,product_no,stock,thumb_img,updated_at,product_id) values (?,?,?,?,?,?,?,?,?,?,?,?,default) insert into product (category_id,created_at,deleted_at,discount_rate,is_own,is_subs,name,price,product_no,stock,thumb_img,updated_at,product_id) values (NULL,NULL,NULL,NULL,NULL,NULL,'빵빵이',1000,'123',NULL,NULL,NULL,default); 2024-01-29T22:28:07.299+09:00 INFO 3636 --- [ main] p6spy : #1706534887299 | took 0ms | rollback | connection 3| url jdbc:h2:mem:~/Marketbridge ; Hibernate: insert into product (category_id, created_at, deleted_at, discount_rate, is_own, is_subs, name, price, product_no, stock, thumb_img, updated_at, product_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, default) 2024-01-29T22:28:07.309+09:00 INFO 3636 --- [ main] p6spy : #1706534887309 | took 0ms | statement | connection 4| url jdbc:h2:mem:~/Marketbridge insert into product (category_id,created_at,deleted_at,discount_rate,is_own,is_subs,name,price,product_no,stock,thumb_img,updated_at,product_id) values (?,?,?,?,?,?,?,?,?,?,?,?,default) insert into product (category_id,created_at,deleted_at,discount_rate,is_own,is_subs,name,price,product_no,stock,thumb_img,updated_at,product_id) values (NULL,NULL,NULL,NULL,NULL,NULL,'빵빵이',1000,'123',NULL,NULL,NULL,default); 2024-01-29T22:28:07.317+09:00 INFO 3636 --- [ main] p6spy : #1706534887317 | took 0ms | rollback | connection 4| url jdbc:h2:mem:~/Marketbridge ; java.lang.AssertionError: [Extracted: productId, productNo, name, price, quantity] Expecting ArrayList: [2L, "123", "빵빵이", 1000L, 2L] to contain: [1L, "123", "빵빵이", 1000L, 2L] but could not find the following element(s): [1L] at com.objects.marketbridge.order.service.dto.ProductListResponseDtoTest.ofWithOrderDetail(ProductListResponseDtoTest.java:44) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) of 메서드 입니다.@Getter public class ProductListResponseDto { private Long productId; private String productNo; private String name; private Long price; private Long quantity; @Builder private ProductListResponseDto(Long productId, String productNo, String name, Long price,Long quantity) { this.productId = productId; this.productNo = productNo; this.name = name; this.price = price; this.quantity = quantity; } public static ProductListResponseDto of(Product product, Long quantity) { return ProductListResponseDto.builder() .productId(product.getId()) .productNo(product.getProductNo()) .name(product.getName()) .price(product.getPrice()) .quantity(quantity) .build(); } public static ProductListResponseDto of(OrderDetail orderDetail) { return ProductListResponseDto.builder() .productId(orderDetail.getProduct().getId()) .productNo(orderDetail.getProduct().getProductNo()) .name(orderDetail.getProduct().getName()) .price(orderDetail.getProduct().getPrice()) .quantity(orderDetail.getQuantity()) .build(); } } 현재 Product 엔티티의 Id는 @GeneratedValue(strategy = GenerationType.IDENTITY) 로 이루어져 있습니다.추가적으로 @ActiveProfiles("test")에 해당하는 yml의 일부는 아래와 같습니다.datasource: url: jdbc:h2:mem:~/Marketbridge driver-class-name: org.h2.Driver username: sa password: jpa: hibernate: ddl-auto: none show-sql: true properties: hibernate: format_sql: true default_batch_fetch_size: 100 defer-datasource-initialization: true h2: console: enabled: true 이런 경우는 처음이라 어디가 잘못됐는지 찾지 못하겠네요 ㅠㅠ
-
미해결자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]
배포 부분 질문드려요!
안녕하세요. 강사님 항상 좋은 강의 감사하게 잘 듣고 있습니다! ec2 프리티어로 강의에서 배포하는 방식으로 실 서비스에서도 바로 사용할 수 있을까요? 해당 강의를 통해 토이 프로젝트를 만들고 실제로 서비스를 하려는 방식으로 사용할 때 강의에서 보여주신 설정이랑 배포방식으로 가능한지 궁금합니다!
-
미해결자바 ORM 표준 JPA 프로그래밍 - 기본편
ex1-hello-jpa-start 실행 오류
[질문 템플릿]1. 강의 내용과 관련된 질문인가요? 예2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? 예3. 질문 잘하기 메뉴얼을 읽어보셨나요? 예[질문 내용]ex1-hello-jpa-start 자료 다운받고, h2 실행한 뒤, JpaMain.java를 실행할때 다음 오류가 떴습니다.
-
해결됨실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
JPA와 DB설정 @GeneratedValue
학습하는 분들께 도움이 되고, 더 좋은 답변을 드릴 수 있도록 질문전에 다음을 꼭 확인해주세요.1. 강의 내용과 관련된 질문을 남겨주세요.2. 인프런의 질문 게시판과 자주 하는 질문(링크)을 먼저 확인해주세요.(자주 하는 질문 링크: https://bit.ly/3fX6ygx)3. 질문 잘하기 메뉴얼(링크)을 먼저 읽어주세요.(질문 잘하기 메뉴얼 링크: https://bit.ly/2UfeqCG)질문 시에는 위 내용은 삭제하고 다음 내용을 남겨주세요.=========================================[질문 템플릿]1. 강의 내용과 관련된 질문인가요? (예)2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)[질문 내용]방금 막 JPA와 DB설정, 동작확인 강의를 들은 수강생입니다.강의를 듣고 제 코드와 비교해보던중 @Entity로 지정된 Member 클래스에서id값을 자동으로 id값을 생성하도록 @GeneratedValue를 붙여주었습니다.이상태로 JpashopApplication의 main을 실행시키면 다음과 같은 오류가 발생합니다.2024-01-29T15:56:56.149+09:00 ERROR 45920 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=jpabook.jpashop.Member]2024-01-29T15:56:56.150+09:00 WARN 45920 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=jpabook.jpashop.Member] @GeneratedValue(strategy = GenerationType.IDENTITY)로 바꿔주니깐 정상적으로 동작하긴 하는데 해결방법이 이게 맞는지 모르겠습니다... 코드는 제공해주신 코드 복붙해서 해보았는데도 동일한 오류 발생합니다 부트 버전 : 3.2.2h2 버전 : 1.4.200사용하고있습니다.
-
미해결Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)
예외처리시 포스트맨에 출력안되는 문제
안녕하세요 ! 강사님좋은 강의 잘 보고 있습니다.Feign의 예외처리에서 강의와 다른부분이 발생하여 질문 남깁니다. FeignErrorDecoder를 통해 예외 처리시 .[user-service] [-auto-1-exec-10] .w.s.m.a.ResponseStatusExceptionResolver : Resolved [org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND "사용자의 주문이 비었습니다"] 위 로그가 user-service에서 error catch를 통해 잘 나오는 것을 확인했습니다.(09:36)강의영상에서는 404 Error를 전달하여 포스트맨 응답에서 Error msg까지 출력이 되었지만, 저의 경우에선 APIGatgeway에서의 로그및 포스트맨응답에Global Filter End : response id -> 403 FORBIDDEN으로 응답이 보내집니다. 이전 강의 부분(정상 응답)에서 해당 문제가 없었지만, 에러 응답시 403응답으로 처리되어지는 부분에 대해서 추가적으로 ApiGateWay에 대해서 github에서는 따로 handling이 없어서 추가적으로 handler를 등록하여 적용되어야하는 부분인지 여쭤봅니다 .++해당부분 user-service의 WebSecurity특정 ip 통과부분에 실제 ip(자신의 ip)를 누락하였네요.Error에 대한 부분도 응답이 됩니다.추가적으로 error에 대해서 403으로 응답이 되는부분은https://colabear754.tistory.com/182해당글을 참고하시면 될것같습니다.
-
미해결Practical Testing: 실용적인 테스트 가이드
이상한 url로 요청해도 200이 반환됩니다. 그리고 body가 비어있어요...
@WebMvcTest(CompanyController::class) @ActiveProfiles("test") @WithMockUser class CompanyControllerTest { @Autowired lateinit var mockMvc: MockMvc @MockBean lateinit var getCompanyService: GetCompanyService @MockBean lateinit var adminAuthorizationFilter: AdminAuthorizationFilter private val cookie = Cookie(ADMIN_AUTH_TOKEN, "admin-auth-token") @DisplayName("회사 목록 조회") @Test fun findAll() { // given val companies = listOf( companyEntity(1, "A"), companyEntity(2, "B"), companyEntity(3, "C"), ) `when`(getCompanyService.findAll()).thenReturn(companies) // when val request = MockMvcRequestBuilders.get("/admin/api/v1/companies") .cookie(cookie).accept(MediaType.APPLICATION_JSON) // then mockMvc.perform(request) .andExpect(MockMvcResultMatchers.status().isOk) .andDo(MockMvcResultHandlers.print()) } } 위 코드를 가지고 질문드리겠습니다. val request = MockMvcRequestBuilders.get("/admin/api/v1/companies") 이 부분에서 companies가 아니라 존재하지 않는 url(ex. /admin/api/v1/cfasfsdfcompanies)라고 한 후 위 코드를 돌리면 200나옵니다. 존재하지 않는 url도 호출이 가능한건가요?그리고 body가 항상 빈 값입니다.MockHttpServletResponse: Status = 200 Error message = null Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"] Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []목킹이 잘못되었는지 점검해보려고 getCompanyService.findAll() 를 호출해보았는데 3개가 정상적으로 나옵니다. 혹시 몰라 소스 공유드립니다.// controller @RestController @RequestMapping("/admin/api/v1/companies") class CompanyController( private val getCompanyService: GetCompanyService ) { @GetMapping fun findAll(): List<CompanyResponse> { return getCompanyService.findAll().map { CompanyResponse.of(it) } } } // service @Service @Transactional(readOnly = true) class GetCompanyService( private val companyFindService: CompanyFindService ) { fun findAll(): List<CompanyEntity> { return companyFindService.findAll() } }
-
미해결Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)
Filter의 로그가 출력되지 않습니다.
gateway에 의해서 api는 정상적으로 호출이 되는데 로그가 출력되지 않습니다.customFilter, globalFilter 모두 출력이 되지 않았습니다. 코드에 혹시 누락된 부분이 있을까요.Global Filterpackage com.example.springgateway.filter; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @Component @Slf4j public class GlobalFilter extends AbstractGatewayFilterFactory<GlobalFilter.Config> { public GlobalFilter() { super(Config.class); } @Override public GatewayFilter apply(Config config) { // Custom Pre Filter return (exchange, chain) -> { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); log.info("Global Filter baseMessage : {}", config.getBaesMessage()); if (config.isPreLogger()) { log.info("Global Filter Start : request id -> {}", request.getId()); } else { log.info("preLogger is false"); } // Custom Post Filter return chain.filter(exchange).then(Mono.fromRunnable(()-> { if (config.isPostLogger()) { log.info("Global Filter End : request id -> {}", response.getStatusCode()); } else { log.info("postLogger is false"); } })); }; } @Data public static class Config { //put the configuration properties private String baesMessage; private boolean preLogger; private boolean postLogger; } } application.ymlspring: application: name: gateway-service cloud: gateway: routes: - id: first-service uri: http://localhost:8081/ predicates: - Path=/first-service/** filters: - CustomFilter - id: second-service uri: http://localhost:8082/ predicates: - Path=/second-service/** filters: - CustomFilter # - AddRequestHeader=second-request, second-request-header2 # - AddResponseHeader=second-response, second-response-header2 - id: user-service uri: http://localhost:8083/ predicates: - Path=/user-service/** filters: - CustomFilter # filters: # - AddRequestHeader=user-request, user-request-header2 # - AddResponseHeader=user-response, user-response-header2 - id: catalog-service uri: lb://CATALOG-SERVICE predicates: - Path=/catalog-service/** - id: order-service uri: lb://ORDER-SERVICE predicates: - Path=/order-service/** default-filters: - name: GlobalFilter args: baseMessage: Spring Cloud Gateway Global Filter preLogger: true postLogger: true eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka server: port: 8000 Filter Configpackage com.example.springgateway.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //@Configuration public class FilterConfig { // @Bean public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/user-service/**") .filters(f -> f.addRequestHeader("user-request", "user-request-header") .addResponseHeader("user-response", "user-response-header")) .uri("http://localhost:8083/")) .route(r -> r.path("/second-service/**") .filters(f -> f.addRequestHeader("second-request", "second-request-header") .addResponseHeader("second-response", "second-response-header")) .uri("http://localhost:8082/")) .build(); } } Postman 실행 결과
-
미해결Practical Testing: 실용적인 테스트 가이드
RestDocs에서의 SpringSecurity Issue
public class FollowControllerDocsTest extends RestDocsSupport { private FollowService followService = mock(FollowService.class); @Override protected Object initController() { return new FollowController(followService); } private final Long loginUserId = 1L; @DisplayName("팔로우 수행 API") @WithUserDetails @Test public void startFollow() throws Exception { // Given StartFollowRequestDto requestDto = StartFollowRequestDto.builder() .followerId(loginUserId) .followingId(123L) .build(); BDDMockito.doReturn(StartFollowResponseDto.success()) .when(followService) .follow(any(StartFollowRequestDto.class)); //when,then mockMvc.perform( post("/api/follow/start-follow") .with(csrf()) .content(objectMapper.writeValueAsString(requestDto)) .contentType(MediaType.APPLICATION_JSON) ) .andExpect(status().isOk()) .andDo(document("start-follow", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestFields( fieldWithPath("followerId").type(JsonFieldType.STRING) .optional() .description("팔로워 아이디"), fieldWithPath("followingId").type(JsonFieldType.STRING) .optional() .description("팔로잉 아이디") ), responseFields( fieldWithPath("code").type(JsonFieldType.STRING) .description(ResponseCode.SUCCESS), fieldWithPath("message").type(JsonFieldType.STRING) .description(SUCCESS) ) )); } } FollowController@RestController @RequestMapping("/api/follow") @RequiredArgsConstructor public class FollowController { private final FollowService followService; @PostMapping("/start-follow") ResponseEntity<? super StartFollowResponseDto> startFollow( @RequestBody @Valid StartFollowRequestDto requestBody, @AuthenticationPrincipal PrincipalDetails principalDetails ) { Long loginId = principalDetails.getUser().getId(); Long userId = requestBody.getFollowerId(); if (loginId != userId) return StartFollowResponseDto.certificationFail(); ResponseEntity<? super StartFollowResponseDto> response = followService.follow(requestBody); return response; } }RestDocsSupport @ExtendWith(RestDocumentationExtension.class) public abstract class RestDocsSupport { protected MockMvc mockMvc; protected ObjectMapper objectMapper = new ObjectMapper(); private final Long loginUserId = 1L; @BeforeEach void setUp(RestDocumentationContextProvider provider) { User user = mock(User.class); when(user.getId()).thenReturn(loginUserId); PrincipalDetails userDetails = mock(PrincipalDetails.class); when(userDetails.getUser()).thenReturn(user); SecurityContext context = SecurityContextHolder.getContext(); context.setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, null)); this.mockMvc = MockMvcBuilders .standaloneSetup(initController()) .apply(MockMvcRestDocumentation.documentationConfiguration(provider)) .build(); System.out.println("beforeEach 수행"); } protected abstract Object initController(); } 안녕하세요. 강의 정말 유익하게 잘 듣고있습니다. 강의를 듣고 restdocs를 작성하던 중 문제가 발생하여 질문을 올립니다.제가 작성한 controller에서는 파라미터로 security.context 안에 있는 PrincipalDetails를 받아옵니다. 그렇지만 테스트에선 그 안에 값이 있는게 아니여서 Mock값을 넣어주어 @WebMvcTest 에서는 문제를 해결해 왔었습니다.문제는 restdocs를 작성하기 위한 테스트 코드에서 발생하였습니다. 이 테스트 코드에는 제가 적용한 PrincipalDetails 몫 이 넣어지지 않는 문제가 있었습니다.제가 생각하기론 SpringSecurity가 올라가지 않아서 그런 것 같은데 해결방법이 있으면 알고싶습니다 !
-
미해결호돌맨의 요절복통 개발쇼 (SpringBoot, Vue.JS, AWS)
@Configuration WebMvcConfig클래스에대해
WebMvcConfig 클래스에 @Configuration애노테이션을 사용하면 addInterceptors 메서드에 @Bean을 달아줘야 동작할줄 알았는데 @Bean 애노테이션을 안달아줘도 잘 작동하는 이유가 궁금합니다.
-
해결됨자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]
1강 질문입니다.
섹션3까지 듣고 만든 유저를 바탕으로 게시판 API만들어보려고 spring.io로 프로젝트를 아예 새로 만들었는데요다른 건 선생님께서 주신 library-app과 유사한데, external libraries에 java11 추가하고 싶은데 추가 하는 방법을 모르겠습니다.또 new해서 새로 만들기 하면 파일, 스크래치 파일, 디렉토리만 있고 class같은 건 없어요..사실 spring.io말고 그냥 intellij에서 new project하면 처음에 설정하는 이 부분에서 generators도 다른 사람은 엄청 많고 여기서 하던데 저는 4개밖에 없는데 왜일까요..?
-
미해결자바 ORM 표준 JPA 프로그래밍 - 기본편
select문도 쓰기 지연에 포함인가요?
[질문 템플릿]1. 강의 내용과 관련된 질문인가요? (예)2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)[질문 내용]안녕하세요. 이전에 질문 남겼는데 좀 대충 작성했더니 질문이 이해 안간다고 하셔서..(ㅠㅠ) 다시 자세하게 작성합니다!질문 : em.find() 하면 select문이 나가잖아요? 이런 경우엔 쓰기 지연이 발생하는게 아닌 것 같다고 생각했는데 맞나요? (동일 트랜잭션 상에서 persist(insert)한 적이 없고, DB에 존재하는 엔티티를 조회한다는 전제입니다.)그렇게 생각한 근거는 1. 이름이 '쓰기' 지연이다. 2. find()한 후에 다음 줄에서 조회한 엔티티를 출력해보면 결과가 나온다.사소한 궁금증인데, 찾아봐도 안나오기도 하고, 강의에서는 update, insert, delete 시 쓰기지연이 발생한다' 라고만 언급하셔서 질문 남깁니다!
-
미해결자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]
8강 서버 내부오류 어떻게 해결하죠..?
sql서버도 껏다가 다시 시작도 해보고포스트맨도 창 전부 지웠다 다시 하고 오류 없이 200ok나온것 확인도 하구인텔리제이 코드도 컴파일 잘 되는거 확인 했습니다..http://localhost:8080/v1/index.html 들어가서 이름 나이 입력하면 서버내부오류라고 나오네요답변해주시면 갑사합니다.그림 첨부하고싶어도 사진클릭이나 저장이 불가능하네요
-
미해결실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
엔티티 - 비즈니스 로직 stockQuantity 질문있습니다!
=====================================[질문 템플릿]1. 강의 내용과 관련된 질문인가요? (예)2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)[질문 내용]상품 엔티티 개발(비즈니스로직) 강의엔티티 내에서 stockQuantity와 관련된 로직이 함께 있어 응집성이 높아짐 + 캡슐화에 부합 -> 관리하기 좋음 이렇게 이해를 했습니다.여기서 질문이 객체지향원칙 SOLID - SRP 단일책임 원칙에 의해 클래스는 하나의 변경 이유만 가져야 한다. 에서 비즈니스 로직을 추가함으로써 SRP 원칙을 위배한거라고도 볼 수 있을까요?맞다면 객체지향원칙 - SRP원칙을 위배했지만 좋은 코드 같긴한데 위 사례에서는 객체지향적 설계로 완벽하다고는 볼 수 없는건가요?객체지향 원칙을 굳이 무조건 따르지 않고 유연하게 코드 개발을 해나가는게 좋을까요?
-
해결됨자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]
api응답을 어떤 식으로 하면 좋을까요?
강의를 보고 혼자서 api를 만들어보고 있습니다.강의 회원 가입 예제api는 void로 별도의 응답을 하지 않았는데 보통은 어떤 식으로 하나요?[{"success" : "true"}]이런 식으로 나마 응답 값을 주는게 맞을까요?