无法处理请求[GET http:// localhost:8080]:响应状态404 Spring,RESTfull,thymleaf

时间:2018-10-19 18:40:16

标签: java spring rest api thymeleaf

我遇到了Spring,Rest和Thymeleafand的问题,我被困在那里,关于此错误的详细信息还很少。

我希望当我以select标记(index.html)的形式选择一个选项时,将其重定向到something.html,但是带有新值(使用api调用),而我只是得到那个它无法处理请求。

我想从HTML表单将值发送到服务和控制器:

index.html:

@Controller
public class DataController {

 private  ApiService apiService;

public DataController(ApiService apiService) {
    this.apiService = apiService;
}

@GetMapping({"", "/", "/index","/cryptos"})
public String index(Model model){

    model.addAttribute("cryptos",apiService.getCrypto(100));

    return "index";
}

@PostMapping(value = "/values/{fiatCurrency}")
public String choseCurrency(Model model,@PathVariable String fiatCurrency){

    model.addAttribute("cryp",apiService.getInDifferentValues(fiatCurrency));

     //returns to the something.html
    return "something";
}
}

控制器类是这样的:

@Service
public class ApiServiceImpl implements ApiService{

private  RestTemplate restTemplate;

@Autowired
public ApiServiceImpl(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
}

@Override
public Map<Integer,Crypto> getCrypto(Integer limit) {

    CryptoData cryptoData = restTemplate.getForObject("https://api.coinmarketcap.com/v2/ticker/?convert=BTC&limit=" + limit , CryptoData.class);


    return cryptoData.getData();
}

@Override
public Map<Integer, Crypto> getInDifferentValues(String fiatCurrency) {

    CryptoData cryptoData = restTemplate.
            getForObject("https://api.coinmarketcap.com/v2/ticker/?convert=" + fiatCurrency + "&limit=100", CryptoData.class);

    return cryptoData.getData();
}
}

服务实现如下:

2018-10-19 20:10:40.147  WARN 15768 --- [ctor-http-nio-4] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/values/?fiatCurrency=EUR]: Response status 404

我是新手,遇到以下错误:

ScriptState

2 个答案:

答案 0 :(得分:1)

根据您的错误堆栈跟踪,

  

2018-10-19 20:10:40.147警告15768 --- [ctor-http-nio-4]   .a.w.r.e.DefaultErrorWebExceptionHandler:无法处理请求   [GET http://localhost:8080/values/?fiatCurrency=EUR]:回复状态   404

您尚未为subprocess.check_call(["""/usr/bin/env bash -c "cd /home/x/y/tools && source /home/x/y/venv/bin/activate && python asdf.py" >> /tmp/asdf.txt 2>&1"""], shell=True) 定义GET映射。您仅为/values操作定义了它,请添加它,它应该可以工作。

答案 1 :(得分:1)

尝试更改此行

**<form th:action="@{/values/}" >

**<form th:action="@{/values/} + ${fiatCurrency}" method="post" >

这会将请求从“获取”更改为“发布”(您现在已经在表单中有了“获取”),并将信息作为pathvariable(在控制器方法中定义)而不是作为请求参数(如就是现在)。

相关问题