繁体   English   中英

Spring 引导控制器返回 404

[英]Spring Boot controllers are returning a 404

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

Spring 引导控制器在所有端点上返回 404

试图获得基本的 controller 返回数据 Package 结构设置正确 所有包都是主要 package 注释的子包找不到 bean idk 没有运气从常规配置中找到任何东西。 请帮忙谢谢

package com.bookieburglar.api.services;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

//@EnableJpaRepositories(basePackages = "com.bookieburlgar.api.services")
@ComponentScan(basePackages = "com.bookieburglar.api.services")
@SpringBootApplication
public class BookieBurglarApplication {

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

}

赔率.java

package com.bookieburglar.api.services.models;

import java.util.List;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Odds {

    @Id
    @JsonProperty("id")
    private String id;

    @JsonProperty("sport_key")
    private String sport_key;

    @JsonProperty("sport_title")
    private String sport_title;

    @JsonProperty("commence_time")
    private String commence_time;

    @JsonProperty("home_team")
    private String home_team;

    @JsonProperty("away_team")
    private String away_team;

    @JsonProperty("bookmakers")
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "odds_id")
    private List<Bookmaker> bookmakers;


    public Odds(String id, String sportKey, String sportTitle, String commenceTime,
            String homeTeam, String awayTeam, List<Bookmaker> bookmakers) {
        this.id = id;
        this.sport_key = sportKey;
        this.sport_title = sportTitle;
        this.commence_time = commenceTime;
        this.home_team = homeTeam;
        this.away_team = awayTeam;
        this.bookmakers = bookmakers;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSportKey() {
        return sport_key;
    }

    public void setSportKey(String sportKey) {
        this.sport_key = sportKey;
    }

    public String getSportTitle() {
        return sport_title;
    }

    public void setSportTitle(String sportTitle) {
        this.sport_title = sportTitle;
    }

    public String getCommenceTime() {
        return commence_time;
    }

    public void setCommenceTime(String commenceTime) {
        this.commence_time = commenceTime;
    }

    public String getHomeTeam() {
        return home_team;
    }

    public void setHomeTeam(String homeTeam) {
        this.home_team = homeTeam;
    }

    public String getAwayTeam() {
        return away_team;
    }

    public void setAwayTeam(String awayTeam) {
        this.away_team = awayTeam;
    }

    public List<Bookmaker> getBookmakers() {
        return bookmakers;
    }

    public void setBookmakers(List<Bookmaker> bookmakers) {
        this.bookmakers = bookmakers;
    }
}

赔率控制器

package com.bookieburglar.api.services.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bookieburglar.api.services.services.OddsAPIService;
import com.bookieburglar.api.services.services.OddsService;
import com.bookieburglar.api.services.models.Odds;
import com.bookieburglar.api.services.repositories.OddsRepository;

@RestController
@RequestMapping("/Odds")
public class OddsController {

    
    

    @Autowired
    private OddsService oddsService;
    
    
    
    
    
    @Autowired
    private OddsAPIService oddsAPIService;

    @GetMapping("/")
    public String getOdds() {
        return "WORLD";
        //return (List<Odds>) OddsRepository.findAll();
    }

//    @GetMapping("/{id}")
//    public Odds getOdds(@PathVariable String id) {
//        return OddsRepository.findById(id).orElse(null);
//    }

    @PostMapping("/create")
    public Odds createOdds(@RequestBody Odds Odds) {
        System.out.println("frthoo");
        return oddsService.saveOdds(Odds);
    }
    
    @GetMapping("/refresh")
    @ResponseBody
    public String refreshOdds() {
        System.out.println("ttgb5");
        //return oddsAPIService.refreshOdds(); 
        return "yoo";
    }

//    @PutMapping("/{id}")
//    public Odds updateOdds(@PathVariable String id, @RequestBody Odds Odds) {
//        Odds.setId(id);
//        return OddsRepository.save(Odds);
//    }
//
//    @DeleteMapping("/{id}")
//    public void deleteOdds(@PathVariable String id) {
//        OddsRepository.deleteById(id);
//    }
    
    
}


赔率服务

package com.bookieburglar.api.services.services;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bookieburglar.api.services.models.Odds;
import com.bookieburglar.api.services.repositories.OddsRepository;

@Service
public class OddsService {

    
    @Autowired
    private OddsRepository oddsRepository;

    public List<Odds> getAllOdds() {
        return (List<Odds>) oddsRepository.findAll();
    }

    public Optional<Odds> findOddsById(String id) {
        return oddsRepository.findById(id);
    }
//    public List<Odds> findOddsBySportTitle(String sportTitle) {
//        return oddsRepository.findBySportTitle(sportTitle);
//    }
//
//    public List<Odds> findOddsByHomeTeam(String homeTeam) {
//        return oddsRepository.findByHomeTeam(homeTeam);
//    }
//    
//    public List<Odds> findOddsByAwayTeam(String awayTeam) {
//        return oddsRepository.findByAwayTeam(awayTeam);
//    }

    public Odds saveOdds(Odds odds) {
        return oddsRepository.save(odds);
    }

    public void deleteOdds(String id) {
        oddsRepository.deleteById(id);
    }
}


赔率资料库

package com.bookieburglar.api.services.repositories;

import java.util.List;


import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;

import com.bookieburglar.api.services.models.Odds;


@Repository
public interface OddsRepository extends JpaRepository<Odds, String> {

    

    
    List<Odds> findAll();
    // additional methods can be defined here, for example, to search for odds by sport key or teams 
}

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bookieburglar.api</groupId>
    <artifactId>services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Bookie Burglar</name>
    <description>API for BookieBurglar</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        
    

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

         <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>



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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>



@ComponentScan注解中。 Package 名称不正确。

@ComponentScan(basePackages = "com.bookieburlgar.api.services")

它应该是

@ComponentScan(basePackages = " com.bookieburglar .api.services")

问题未解决?试试搜索: Spring 引导控制器返回 404
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM