Spring引导休息:循环视图路径[error]:将再次发送回当前处理程序URL [/ error]

时间:2016-10-02 21:20:56

标签: spring-boot spring-restcontroller

我的问题是在localhost:8080 / users

上调用spring boot应用程序时出现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/中测试了弹簧项目并且它有效,所以我忽略了正在发生的事情。

1 个答案:

答案 0 :(得分:0)

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

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。