2016-09-06 72 views
1

我想製作一個簡單的java應用程序,我想使用CRUDREPOSITORY和我自己的存儲庫。我有這樣的:SpringBoot Rest api

@RestController 
@Transactional 
@ExposesResourceFor(Person.class) 
@RequestMapping("/prueba") 
public class PersonController { 

    @RequestMapping(value="/prueba", method=RequestMethod.GET) 
    public String person(@RequestParam(value="id", defaultValue="1") int id) {  
    return "hola"+id; 
} 

} 

這樣的:

@RepositoryRestResource 
public interface IClientRepository extends CrudRepository<Client, Long> { 
} 

的問題是,CRUD效果很好,但我不能叫我的本地主機:8080/prueba/prueba,因爲它給出了一個404錯誤。我嘗試了所有,我無法訪問它,請幫助我!

+0

你是如何運行的? – chrylis

+0

作爲Spring Boot應用:) –

+0

當你啓動你的Spring應用程序時,它將列出它從你的控制器和任何自動配置的東西(即@RepositoryRestResource)找到的請求映射。那裏是否有'/ prueba/prueba'的映射? –

回答

1

默認情況下,Spring Data REST在根URI「/」處提供REST資源。有多種方法可以更改基本路徑。

隨着春天引導1.2+,下面添加到application.properties:

spring.data.rest.basePath=/api 

你的情況: spring.data.rest.basePath =/prueba/prueba ,假設沒有覆蓋服務器.contextPath在application.properties

參考: http://docs.spring.io/spring-data/rest/docs/current/reference/html/

+0

最初的問題是CRUD運行良好(公開REST資源)。問題是額外的REST端點沒有在控制器類和方法的註釋中定義的'/ prueba/prueba'處返回一個值。 –

+0

這個問題並沒有說CRUD在根路徑上還是在預期的路徑上運行良好。 – Stacky