为啥我用 spring-boot 得到 404 休息

Posted

技术标签:

【中文标题】为啥我用 spring-boot 得到 404 休息【英文标题】:Why do I get 404 for rest with spring-boot为什么我用 spring-boot 得到 404 休息 【发布时间】:2016-07-04 00:15:48 【问题描述】:

我正在使用 Spring Boot 实现休息服务。实体类在单独的包中定义。所以我在 Application.java 中添加了 Component 注释。

@Configuration
@EnableAutoConfiguration
@ComponentScan("org.mdacc.rists.cghub.model")
@EnableJpaRepositories(basePackages = "org.mdacc.rists.cghub.model") 
public class Application 

    public static void main( String[] args )
    
        SpringApplication.run(Application.class, args);
    

这是我的控制器类:

// SeqController.java
@RestController
public class SeqController 

    @Autowired
    private SeqService seqService;
    @RequestMapping(
            value = "/api/seqs", 
            method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<SeqTb>> getSeqs() 
        List<SeqTb> seqs = seqService.findAll();

        return new ResponseEntity<List<SeqTb>>(seqs, HttpStatus.OK);
    

我还创建了一个扩展 JPARepository 的 JPA 数据存储库,我在其中添加了自定义查询代码。

// SeqRepository.java
@Repository
public interface SeqRepository extends JpaRepository<SeqTb, Integer> 

    @Override
    public List<SeqTb> findAll();

    @Query("SELECT s FROM SeqTb s where s.analysisId = :analysisId")
    public SeqTb findByAnalysisId(String analysisId);

下面是实现服务接口的servicebean类

// SeqServiceBean.java
@Service
public class SeqServiceBean implements SeqService 
    @Autowired
    private SeqRepository seqRepository;

    @Override
    public List<SeqTb> findAll() 
        List<SeqTb> seqs = seqRepository.findAll();
        return seqs;
    

    public SeqTb findByAnalysisId(String analysisId) 
        SeqTb seq = seqRepository.findByAnalysisId(analysisId);
        return seq;
    

当我启动应用程序并在浏览器中输入以下 url "http://localhost:8080/api/seqs" 时,出现 404 错误。我错过了什么?

编辑#1: 我决定取出 JPA 存储库的东西并将控制器类更改为以下内容:

@RestController
//@RequestMapping("/")
public class SeqController 
    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting) 
        if(greetingMap == null) 
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    

    static 
        Greeting g1 = new Greeting();
        g1.setText("Hello World!");
        save(g1);

        Greeting g2 = new Greeting();
        g1.setText("Hola Mundo!");
        save(g2);

    
    @RequestMapping(
            value = "/api/greetings", 
            method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Greeting>> getGreetings() 
        Collection<Greeting> greetings = greetingMap.values();
        return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);

    

当我启动应用程序并在浏览器中输入“localhost:8080/api/greetings”时,我仍然得到 404。

【问题讨论】:

您的 application.properties 中是否设置了内容根路径?你也介意分享一下吗? @shahshi15 我的application.properties中只有以下几行:spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://mdarisrac02d:3306/pancancer spring.datasource.username=user spring.datasource.password=pass spring.jpa.hibernate.ddl-auto=update如何设置内容根路径? 【参考方案1】:

==>您是否确保您的 Spring Boot 应用程序类和您的 Rest Controller 在同一个基础包中?例如,如果您的 Spring Boot 应用程序类的包是 com.example.demo,那么您的 Rest Controller 应该与 com.example.demo.controller 在同一个基础包中。

==>我认为这就是 boot 无法映射到您的 rest 控制器的 uri 的原因。因为@SpringBootApplication 已经嵌入了@ComponentScan 和@Configuration。尝试这样做。我希望它有效。

【讨论】:

我认为这很好@Billa。如果需要任何更改,请告诉我【参考方案2】:

如果你的 pom.xml 中没有 spring boot starter web,那么添加相同的原因可能是代码无法映射端点。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

【讨论】:

【参考方案3】:

我会尝试的第一件事是将@RequestMapping("/") 放在控制器的类定义中。在方法上保持相同的值。

与您的问题无关的另一件事是您不需要定义该自定义查询。 JPA 实际上足够聪明,可以通过使用该方法名称来执行您定义的查询。在此处查看findByLastName 示例:https://spring.io/guides/gs/accessing-data-jpa/。

【讨论】:

我在控制器类之前添加了该行,但没有帮助。实际上我刚刚发现只有“localhost:8080”我得到了 404。

以上是关于为啥我用 spring-boot 得到 404 休息的主要内容,如果未能解决你的问题,请参考以下文章

为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?

为啥spring-boot应用程序不需要@EnableWebMvc

为啥我得到一个 404 到 laravel 5.7 公共子文件夹?

为啥我不能使用类别详细视图?如果在django中使用它得到404?

为啥我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面

如何在 spring-boot 中拦截嵌入式 Tomcat 上的“全局”404