请求ID找不到白色的findbyid方法作为requestparam

时间:2018-09-19 21:35:48

标签: java spring hibernate spring-boot repository

我正在做连接到MySQL数据库的服务,并且我在请求参数的方法上遇到问题。但似乎看不懂。

我的意思是,在我执行的打印中,它返回参数。但是在findbyId方法中找不到所需的文件。

MainController方法:

@Controller
@RequestMapping(path="/festivales")
public class MainController {

@Autowired
private FestivalRepository festivalRepository;

@Autowired
private UsuarioRepository usuarioRepository;

//http://localhost:8080/festivales/all-festivales
@GetMapping(path="/all-festivales")
public @ResponseBody Iterable<Festival> getAllFestivales() {
    // This returns a JSON or XML with the users
    return festivalRepository.findAll();
}

//Doesn't work
    @GetMapping("/all-festivales/{id}")
    public Festival getFestival(@PathVariable int id) throws Exception {
        Optional<Festival> student = festivalRepository.findById(id);

        if (!student.isPresent())
            throw new Exception("id-" + id);

        System.out.println(id);
        System.out.println(student.get());
        System.out.println(student.get().toString());


        return student.get();
    }   

有助于了解我要执行的操作的其他方法:

班级节日:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Festival {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    private String descripcion;
    private String genero;
    private double precio;
    private int dias;
    private String fecha_ini;
    private String fecha_fin;

    public Festival() {}

    public Festival(int id, String name, String descripcion, String genero, double precio, int dias, String fecha_ini, String fecha_fin) {
        this.id = id;
        this.name = name;
        this.descripcion = descripcion;
        this.genero = genero;
        this.precio = precio;
        this.dias = dias;
        this.fecha_ini = fecha_ini;
        this.fecha_fin = fecha_fin;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getGenero() {
        return genero;
    }

    public void setGenero(String genero) {
        this.genero = genero;
    }

    public double getPrecio() {
        return precio;
    }

    public void setPrecio(double precio) {
        this.precio = precio;
    }

    public int getDias() {
        return dias;
    }

    public void setDias(int dias) {
        this.dias = dias;
    }

    public String getFecha_ini() {
        return fecha_ini;
    }

    public void setFecha_ini(String fecha_ini) {
        this.fecha_ini = fecha_ini;
    }

    public String getFecha_fin() {
        return fecha_fin;
    }

    public void setFecha_fin(String fecha_fin) {
        this.fecha_fin = fecha_fin;
    }
}

存储库:

import org.springframework.data.repository.CrudRepository;
import com.example.pruebas.festivalapp.model.Festival;

//This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
//CRUD refers Create, Read, Update, Delete

public interface FestivalRepository extends CrudRepository<Festival, Integer> {

}

另外,说我正在尝试执行的服务不会崩溃,并且当我执行maven安装或运行该应用程序时,一切正常。

这是我放入本地主机攻击的MySQL数据:

当我选择获取所有节日时的json:

[
  {
    "id": 1,
    "name": "Viñarock",
    "descripcion": "Festival de rock rap y mestizaje",
    "genero": "rock",
    "precio": 50.0,
    "dias": 3,
    "fecha_ini": "10-05-2019",
    "fecha_fin": "12-05-2019"
  },
  {
    "id": 2,
    "name": "Resurrection Fest",
    "descripcion": "Festival de rock, metal, hardcore..",
    "genero": "rock",
    "precio": 80.0,
    "dias": 3,
    "fecha_ini": "10-07-2019",
    "fecha_fin": "12-07-2019"
  },
  {
    "id": 3,
    "name": "MareaRock Fest",
    "descripcion": "Festival de punk rock",
    "genero": "punk",
    "precio": 15.0,
    "dias": 1,
    "fecha_ini": "10-04-2019",
    "fecha_fin": "12-04-2019"
  },
  {
    "id": 4,
    "name": "Festardor",
    "descripcion": "Festival de punk y rap",
    "genero": "otros",
    "precio": 20.0,
    "dias": 2,
    "fecha_ini": "10-09-2019",
    "fecha_fin": "12-09-2019"
  },
  {
    "id": 5,
    "name": "Festival de les arts",
    "descripcion": "Festival indie",
    "genero": "otros",
    "precio": 40.0,
    "dias": 2,
    "fecha_ini": "10-06-2019",
    "fecha_fin": "12-06-2019"
  }
]

有人可以帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您为两个终点提供了相同的路径“ all-festivales / {id},我不知道应用程序如何运行,因为这会导致模棱两可,因此您必须在同一请求方法中提供唯一的路径,因此删除第一个并使用第二个@PathVariable应该可以工作