Spring boot rest:循环视图路径[error]:将再次分派回当前处理程序URL [/error]

Spring boot rest : Circular view path [error]: would dispatch back to the current handler URL [/error] again

我的问题是在 localhost:8080/users

上调用 spring 引导应用程序时出现 404 错误
package com.myproj.users.controller;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.security.Principal;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.myproj.users.Greeting;
import com.myproj.users.PhysicalCharacteristicsRepository;
import com.myproj.users.UserRepository;
import com.myproj.users.UserResource;

@RestController
@RequestMapping("/users")
public class UserRestController {

    private UserRepository userRepository;
    private PhysicalCharacteristicsRepository characteristicsRepository;

    @RequestMapping(value = "/greeting/", method = RequestMethod.GET)
    public String greeting() throws UserPrincipalNotFoundException {
        return "Greeting";
    }

    @RequestMapping(value = "/error/")
    public String error() {
        return "Error handling";
    }

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Greeting sayHello(@RequestParam(value = "name", required = false, defaultValue = "Stranger") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

    @Autowired
    UserRestController(UserRepository userRepository, PhysicalCharacteristicsRepository characteristicsRepository) {
        this.userRepository = userRepository;
        this.characteristicsRepository = characteristicsRepository;
    }
}


package com.myproj.users.controller;

import java.nio.file.attribute.UserPrincipalNotFoundException;

import org.springframework.hateoas.VndErrors;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.weather.exceptions.UserNotFoundException;

@ControllerAdvice
class UserControllerAdvice {

    @ResponseBody
    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors userNotFoundExceptionHandler(UserNotFoundException ex) {
        return new VndErrors("error", ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(UserPrincipalNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors userPrincipalNotFoundException(UserPrincipalNotFoundException ex) {
        return new VndErrors("error", ex.getMessage());
    }
}

package com.myproj.users;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

}

我已经在 https://spring.io/guides/gs/actuator-service/ 中测试了 spring 项目并且它有效,所以我忽略了正在发生的事情。

我已经定义了一个控制器来管理错误。我从 Spring Boot Remove Whitelabel Error Page 复制了它 新的应用程序 class 如下:

package com.test;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@ComponentScan(basePackages = "com.test")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.test")
@EntityScan(basePackages = "com.test")
public class Application {

    static final Logger logger = LogManager.getLogger(Application.class.getName());

    public static void main(String[] args) {
        logger.debug("Entered the application");
        SpringApplication.run(Application.class, args);
    }

    private Application() {
    }
}

如您所见,我在 ComponentScan 中添加了一个控制器,如下所示:

 @ComponentScan(basePackages = "com.test")



@EnableJpaRepositories(basePackages = "com.test")
@EntityScan(basePackages = "com.test")

为了测试,我使用了 curl curl http://localhost:9002/eleves/Hammami/ 和 firefox。