我正在使用 Java Spring 框架从我的一部分数据中映射数据,我的路径变量不会让我访问任何数据

I'm using Java Spring framework to map data from a portion of my data and my pathvariable won't get me access to any data

我正在使用 Java Spring 框架从我的一部分数据映射数据,而我的@pathvariable 不会让我访问任何数据。我进入控制台的所有内容是 bash: localhost:4001/traveladventures/bycountry/Greece: No such file or directory

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;

  @GetMapping()
  public Iterable<Adventure> getAllAdventures(){
    return this.adventureRepository.findAll();
  }

  @GetMapping("/bycountry/{country}")
  public List<Adventure> getByCountry(@PathVariable("country") String country){
    return this.adventureRepository.findByCountry(country);
  }

我的数据通过 sql 进入,我正在使用此接口访问数据

public interface AdventureRepository extends CrudRepository<Adventure, Integer> {
  public List<Adventure> findByCountry(String country);
  public List<Adventure> findByState(String state);
}

你能否确认你是否在这个 getByCountry() 方法中获得了国家的价值(通过日志记录或调试),或者它甚至没有达到 API? 另外,请检查 API 的路径。第一个 Get API 有效吗?

另外一个建议,Controller基本上是API的哑入口点,所有的业务逻辑都必须在Service层实现。控制器只是将传入的值传递给服务层。

试试这个命令

Blockquote

curl -v http://localhost:4001/traveladventures/bycountry/Greece