Java Spring - Null returns repository

Asked

Viewed 159 times

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

Stackoverflow in English - @Service, @Transactional

Estrutura do projeto

  • It would be interesting to quote which solutions have already tried and not worked.

  • 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?

  • Climatempoapi wouldn’t have a @Service? Where do you say Climatempoapi is managed by spring to make dependency injection?

  • @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.

  • @Service, @Component and @Repository perform the same function (make class one bean to be discovered by Autoscan spring). Your note is correct there.

  • I don’t know if that would be a problem: The Climatempoapi.java class is in a package above the controller that calls it...

  • You can post the package structure of your application?

  • Yes, I updated the question

  • APILux is where your main?

  • Exactly... The rest of the project works normally (but in other Apis I’m not using Apikeyrepository)

  • Which class uses ClimaTempoAPI? Ask her the question. Share also whole the Nullpointerexception error.

Show 7 more comments

1 answer

2


I don’t know why, but it only solved when I passed the code to the Climatempocontroller class.

Climatempocontroller.java:

    @RestController
    @RequestMapping("/cockpit")
    @CrossOrigin(origins="*", maxAge=3600)
    public class ClimaTempoController { 

        @Autowired
        ApiKeyRepository apiKeyRepository;  

        @RequestMapping(value= "/clima/{nomeCidade}/{ufCidade}/agora",  method = {RequestMethod.GET})
        public ResponseEntity<Clima> getClimaAgoraByNomeCidade(@PathVariable etc etc..) {
            (...)
            ApiKey appToken = apiKeyRepository.getByservice("climaTempo2");
            climaCidade = ct.RequestWeather(weatherEndpoint, appToken.getKey());

        }
     }

The same code only in a @Restcontroller class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.