본문 바로가기

Spring

cannot find method 'value'

 


 

@Value 컴파일 에러 발생한다

빨간줄 보이죠?

 

아이디랑 비번을 프로퍼티에 담아서 @Value 로 불러오면 되는 건데 왜 컴파일 에러가 나지?

 

빨간색에 마우스를 가져다보자


cannot find method 'value'


원인: import를 잘못 가져와서 그렇다

 

import lombok.Value

롬복이 아닌 아래 import 가져와야 한다.

 

import org.springframework.beans.factory.annotation.Value;

 


 

예방: Setting -> Enable annotation processing 체크

 


 

생성자 주입으로 코드를 수정해주고..

@RequiredArgsConstructor 지양

이유는 회사에서 시니어분이 @RequiredArgsConstructor 이런거 쓰면 스프링이 자기가 대신 만들어주는데

원하지 않는 상황이 올 수 도 있다고 해서

생성자 직접 작성함

package com.sparta.myselectshop.naver.service;

import com.sparta.myselectshop.naver.dto.ItemDto;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Slf4j(topic = "NAVER API")
@Service
public class NaverApiService {

	private final String clientId;
	private final String clientSecret;
	private final RestTemplate restTemplate;

	// 생성자 주입
	public NaverApiService(@Value("${naver.client.id}") String clientId,
	                       @Value("${naver.client.secret}") String clientSecret,
	                       RestTemplate restTemplate) {
		this.clientId = clientId;
		this.clientSecret = clientSecret;
		this.restTemplate = restTemplate;
	}

	public List<ItemDto> searchItems(String query) throws JSONException {
		// 요청 URL 만들기
		URI uri = UriComponentsBuilder
				.fromUriString("https://openapi.naver.com")
				.path("/v1/search/shop.json")
				.queryParam("display", 15)
				.queryParam("query", query)
				.encode()
				.build()
				.toUri();
		log.info("uri = " + uri);

		RequestEntity<Void> requestEntity = RequestEntity
				.get(uri)
				.header("X-Naver-Client-Id", clientId)
				.header("X-Naver-Client-Secret", clientSecret)
				.build();

		ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);

		log.info("NAVER API Status Code : " + responseEntity.getStatusCode());

		return fromJSONtoItems(responseEntity.getBody());
	}

	public List<ItemDto> fromJSONtoItems(String responseEntity) throws JSONException {
		JSONObject jsonObject = new JSONObject(responseEntity);
		JSONArray items  = jsonObject.getJSONArray("items");
		List<ItemDto> itemDtoList = new ArrayList<>();

		for (int i = 0; i < items.length(); i++) {
			JSONObject item = items.getJSONObject(i);
			ItemDto itemDto = new ItemDto(item);
			itemDtoList.add(itemDto);
		}

		return itemDtoList;
	}
}

 

 

application.properties --> 애는 깃에 올리기 전에 ignore에 추가 해야 함

spring.application.name=myselectshop
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=비번
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
jwt.secret.key=키

# Hibernate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

# naver api
naver.client.id=아이디
naver.client.secret=비번

 

 


결과: naver api를 통해서 mac으로 검색한 결과를 가져올 수 있다.