2
This is my Apikey class:
@Entity
@Table(name="API_KEYS", schema="DEMO_PIMS")
@JsonIgnoreProperties(allowGetters=true)
public class ApiKey implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="ID_REG")
private Integer id;
@Column(name="SERVICE")
private String service;
@Column(name="KEY")
private String Key;
(... Getters and Setters...)
This is the repository:
@Repository
public interface ApiKeyRepository extends JpaRepository<ApiKey, Integer>{
@Query("SELECT a FROM ApiKey a WHERE a.service = ?1")
ApiKey getByservice(String nome);
}
I am using the Climatempoapi.java class which has the following structure:
public class ClimaTempoAPI {
@Autowired
ApiKeyRepository apiKeyRepository;
public JSONObject RequestWeather(String weatherEndpoint) throws IOException, JSONException, ParseException {
ApiKey appToken = apiKeyRepository.getByservice("climaTempo2");
URL weatherDomain = new URL("http://apiadvisor.climatempo.com.br" + weatherEndpoint + appToken.getKey());
return ConnectionJson.returnJson(weatherDomain, true);
}
}
But when calling the getByservice method("climaTempo2") it throws an exception from Null Pointer Exception.
What I’m doing wrong that doesn’t make it work?
I’ve seen other answers on Stackoverflow, but what they put as a solution didn’t work for me:
Stackoverflow - Tested by adding @Service, @Configurable and @Component - Failed
It would be interesting to quote which solutions have already tried and not worked.
– StatelessDev
Another thing: the class containing the method
main
(where you probably used the note@SpringBootApplication
) of your application is in a package above all other?– StatelessDev
Climatempoapi wouldn’t have a @Service? Where do you say Climatempoapi is managed by spring to make dependency injection?
– Bruno Spy
@Statelessdev as to main is already like that you said. In the code I did not put, but I tried using the @Service, @ Component annotations and gives the same error.
– WitnessTruth
@Service
,@Component
and@Repository
perform the same function (make class one bean to be discovered by Autoscan spring). Your note is correct there.– StatelessDev
I don’t know if that would be a problem: The Climatempoapi.java class is in a package above the controller that calls it...
– WitnessTruth
You can post the package structure of your application?
– StatelessDev
Yes, I updated the question
– WitnessTruth
APILux
is where yourmain
?– StatelessDev
Exactly... The rest of the project works normally (but in other Apis I’m not using Apikeyrepository)
– WitnessTruth
Let’s go continue this discussion in chat.
– StatelessDev
Which class uses
ClimaTempoAPI
? Ask her the question. Share also whole the Nullpointerexception error.– Dherik