如何为PersistentEntityResourceAssembler定义一个bean?

时间:2017-05-10 13:45:43

标签: spring spring-mvc spring-boot spring-data-rest

当我尝试在自定义控制器中自动装配PersistentEntityResourceAssembler时,它会给我以下错误。

    private string ValidatePasswordComplexity(string newPassword)
    {
        StringBuilder errorMessage = new StringBuilder();

        var hasNumber = new Regex(@"[0-9]+");
        var hasUpperChar = new Regex(@"[A-Z]+");
        var hasLowerChar = new Regex(@"[a-z]+");
        var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
        var criteria = 0;

        if (hasUpperChar.IsMatch(newPassword))
            criteria++;
        else 
        {
            errorMessage.Append("Password should contain at least one English uppercase letter (A through Z).");
            errorMessage.AppendLine();
        }

        if (hasLowerChar.IsMatch(newPassword))
            criteria++;
        else
        {
            errorMessage.Append("Password should contain at least one English lowercase letter (a through z).");
            errorMessage.AppendLine();
        }

        if (hasNumber.IsMatch(newPassword))
            criteria++;
        else
        {
            errorMessage.Append("Password should contain at least one numeric value (0 through 9).");
            errorMessage.AppendLine();
        }

        if (hasSymbols.IsMatch(newPassword))
            criteria++;
        else
        {
            errorMessage.Append("Password should contain at least one non-alphabetic character (for example: !, $, #, %).");
        }

        if (criteria >= 3)
            return "Success";
        else
            return errorMessage.ToString();
    }

这是我的控制器实现:

Description:

Field resourceAssembler in api.controller.IslandController required a bean of type 'org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler' in your configuration.

那么如何为PersistentEntityResourceAssembler定义一个bean?

1 个答案:

答案 0 :(得分:1)

这不是解决这个问题的方法:摆​​脱@Autowired PersistentEntityResourceAssembler并将PersistentEntityResourceAssembler参数传递给你的方法,让Spring发挥其魔力

@RequestMapping(method = GET, value = "islands")
public ResponseEntity<?> getAllIslands(Pageable page,
        PersistentEntityResourceAssembler resourceAssembler) {
    Page<Island> islandList = islandRepo.findAll(page);

    return new ResponseEntity<>(pagedResourcesAssembler.toResource(islandList, resourceAssembler), HttpStatus.OK);
}