Springboot and IDEA error: Could not autowire. No beans of 'EntityLinks' type found

Viewed 726

I am following Spring in action 5th edition to learn Springboot. When I am in chapter 6, I find that my IDEA IDE seems to have a bug for bean org.springframework.hateoas.server.EntityLinks.

package tech.enigma.web.api;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.enigma.Taco;
import tech.enigma.data.TacoRepository;


@RestController
@RequestMapping(path = "/design", produces = "application/json")
@CrossOrigin(origins = "*")
public class DesignTacoController
{
    private TacoRepository tacoRepo;
    private EntityLinks entityLinks;

    public DesignTacoController(TacoRepository tacoRepo, EntityLinks entitylinks)
    {
        this.tacoRepo = tacoRepo;
        this.entityLinks = entitylinks;
    }

    @GetMapping("/recent")
    public Iterable<Taco> recentTacos()
    {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createAt").descending());

        return tacoRepo.findAll(page).getContent();
    }
}

At public DesignTacoController(TacoRepository tacoRepo, EntityLinks entitylinks) Constructor, IDEA gives an error "Could not autowire. No beans of 'EntityLinks' type found." I can compile and run my program although I am not sure it works properly. Other beans all works ok. Is this just a bug of IDEA or I got something wrong?

2 Answers

This is a known issue. IntelliJ will not always pickup all available beans due to incorrect scanning of autoconfigured resources. What counts is the Spring runtime. If it doesn't result in an error, you are good to go.

@Autowired annotation is missing. Either do constructor injection or setter injection then it will work.

package tech.enigma.web.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.enigma.Taco;
import tech.enigma.data.TacoRepository;


@RestController
@RequestMapping(path = "/design", produces = "application/json")
@CrossOrigin(origins = "*")
public class DesignTacoController
{
    @Autowired
    private TacoRepository tacoRepo;
    @Autowired
    private EntityLinks entityLinks;

   

    @GetMapping("/recent")
    public Iterable<Taco> recentTacos()
    {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createAt").descending());

        return tacoRepo.findAll(page).getContent();
    }
}
Related