0
In the example below, I am trying to make a Restful Service by fully separating @Restcontroller, @Service and @Repository. But I’m having a difficulty, because when I try to use the service inside the controller it always stays NULL, so when trying to use it gives a NULL EXCEPTION.
I must use Repository inside the controller, or I can continue to use more separately ?
Service
package com.pokemon.tcg.api.Services;
import com.pokemon.tcg.api.Models.Type; import com.pokemon.tcg.api.Repositories.Typerespository; import org.springframework.Beans.factory.Annotation.Autowired; import org.springframework.Beans.factory.Annotation.Configurable; import org.springframework.stereotype.Service;
@Service @Configurable public class Typeservice { @Autowired TypeRespository typeRespository;
public void save(Type type){
typeRespository.save(type);
}
} Restcontroller
package com.pokemon.tcg.api.Controllers;
import com.pokemon.tcg.api.Models.Type; import com.pokemon.tcg.api.Services.Typeservice; import org.springframework.Beans.factory.Annotation.Autowired; import org.springframework.http.Httpstatus; import org.springframework.http.Mediatype; import org.springframework.http.Responseentity; import org.springframework.web.bind.Annotation.*;
@Restcontroller @Requestmapping("/types") public class Typecontroller {
@Autowired
private TypeService typeService;
@Requestmapping(value = "/save", method = Requestmethod.POST, consumes = Mediatype.APPLICATION_JSON_UTF8_VALUE, produces = Mediatype.APPLICATION_JSON_UTF8_VALUE ) @Responsebody public Responseentity save(@Requestbody Type type){
typeService.save(type);
return new ResponseEntity<>(type, HttpStatus.CREATED);
}
}
Repository
package com.pokemon.tcg.api.Repositories;
import org.springframework.data.repository.Crudrepository; import com.pokemon.tcg.api.Models.Type;
public interface Typerespository extends Crudrepository { }