为什么我用 spring-boot 得到 404 来休息

Nas*_*din 4 java rest spring-data-jpa spring-boot

我正在使用 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器类:

// 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还创建了一个 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);
}
Run Code Online (Sandbox Code Playgroud)

下面是实现服务接口的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序并在浏览器中输入以下网址“ 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);

    }
}
Run Code Online (Sandbox Code Playgroud)

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

Jay*_*Jay 6

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

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