gpt4 book ai didi

java - 无法处理请求 [GET http ://localhost:8080 ]: Response status 404 Spring, RESTful,thymeleaf

转载 作者:行者123 更新时间:2023-12-02 01:51:45 27 4
gpt4 key购买 nike

我遇到了 Spring、Rest 和 Thymeleaf 的问题,我被困在那里,并且没有太多关于我遇到的错误的详细信息。

我希望当我在表单中选择选择标记(index.html)中的选项之一时,将其重定向到something.html,但使用新值(使用api调用),我刚刚得到了它无法处理该请求。

我想将值从 html 表单发送到服务和 Controller :

index.html:

<body>
<!--/*@thymesVar id="cryptos" type="java.util.Map<Integer, jasmin.merusic.domain.Crypto>"*/-->
<!--/*@thymesVar id="crypto" type="jasmin.merusic.domain.Crypto"*/-->
<div class="container-fluid" style="margin-top: 20px">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">

<div class="panel-heading">
<h1 class="panel-title">CryptoCurrencies from API</h1>
</div>
<div class="panel-body">
<div class="table-responsive" th:if="${not #maps.isEmpty(cryptos)}">
<table class="table table-hover ">
<thead class="thead-inverse">
<tr>
<th>Name</th>
<th>
**<form th:action="@{/values/}" >
<select name="fiatCurrency" onchange="this.form.submit()">
<option selected="selected" value="USD">USD</option>
<option value="EUR">Euro</option>
<option value="CNY">C. Yuan</option>
</select>
</form>**
</th>
<th>Change in 24h(%)</th>
<th>Rank</th>
<th>Symbol</th>
</tr>
</thead>
<tr th:remove="all">
<td>Joe</td>
<td>Buck</td>
<td>Male</td>
<td>foo@example.com</td>
</tr>
<tr th:remove="all">
<td>Joe</td>
<td>Buck</td>
<td>Male</td>
<td>foo@example.com</td>
</tr>

<tr th:each="crypto : ${cryptos.values()}">
<td th:text="${crypto.name}">Joe</td>
<span th:each="cryp : ${crypto.quotes.values()}">
<td th:text="${cryp.price}">Buck</td>
<td th:text="${cryp.percent_change_24h}">Buck</td>
</span>
<td th:text="${crypto.rank}">Male</td>
<td th:text="${crypto.symbol}">foo@example.com</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</body>

Controller 类是这样的:

@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

最佳答案

尝试更改此行

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

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

这会将请求从“get”更改为“post”(表单中现在有“get”),并将信息作为路径变量(如 Controller 方法中定义)发送,而不是作为请求参数(如就是现在)。

关于java - 无法处理请求 [GET http ://localhost:8080 ]: Response status 404 Spring, RESTful,thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52898237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com