Java 为什么我要用弹簧靴休息404

Java 为什么我要用弹簧靴休息404,java,rest,spring-boot,spring-data-jpa,Java,Rest,Spring Boot,Spring Data Jpa,我正在用Spring Boot实现rest服务。实体类在单独的包中定义。所以我在Application.java中添加了组件注释 @Configuration @EnableAutoConfiguration @ComponentScan("org.mdacc.rists.cghub.model") @EnableJpaRepositories(basePackages = "org.mdacc.rists.cghub.model") public class Application {

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

@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);
    }
}
我还创建了一个JPA数据存储库,它扩展了JPARepository,并在其中添加了自定义查询代码

// 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时,出现了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。

我要尝试的第一件事是在控制器的类定义上添加@RequestMapping/。在方法上保持相同的值


另一件与您的问题无关的事情是,您不需要定义自定义查询。JPA实际上足够聪明,只需使用该方法名即可执行您定义的查询。查看此处的findByLastName示例:。

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

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

==>您是否确保Spring Boot应用程序类和Rest控制器位于同一个基本包中?例如,如果Spring Boot应用程序类的包是com.Example.demo,那么Rest控制器应该与com.Example.demo.Controller位于相同的基本包中


==>我认为这就是boot无法映射到rest控制器的uri的原因。因为@SpringBootApplication中已经嵌入了@ComponentScan和@Configuration。试着这样做。我希望它能起作用。

我在我的controller类之前添加了这一行,但没有任何帮助。实际上,我刚刚发现,仅使用localhost:8080,我就得到了404。您的应用程序中是否设置了内容根路径。属性?你介意分享吗?@shahshi15我在application.properties中只有以下几行:spring.datasource.drivercassname=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如何设置内容根目录路径?我认为这是好的,比拉。如果需要任何更改,请告诉我