2016-10-02 18 views
0

我的問題是調用本地主機上的春天啓動應用程序時,我得到404錯誤:8080 /用戶春天開機休息:圓形視覺路徑[錯誤]:將分發回當前處理URL [/錯誤再次

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/的彈簧項目,它工作,所以我忽略了發生了什麼。

回答

0

我已經定義了一個控制器來管理錯誤。我從Spring Boot Remove Whitelabel Error Page 新的應用類複製它是這樣的:

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 http://localhost:9002/eleves/Hammami/和Firefox瀏覽器。