![](/img/trans.png)
[英]get response status code tells if the request is supposed to responded with HTTP 404 inside a Spring MVC Interceptor preHandle method
[英]Failed to handle request [GET http://localhost:8080 ]: Response status 404 Spring, RESTfull ,thymleaf
我遇到了Spring,Rest和Thymeleafand的問題,我被困在那里,有關此錯誤的詳細信息很少。
我希望當我以select標記(index.html)中的選項之一的形式選擇重定向到something.html,但是具有新值(帶有api調用),而我得到的只是它可以處理請求。
我想從HTML表單將值發送到服務和控制器:
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
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
根據您的錯誤堆棧跟蹤,
2018-10-19 20:10:40.147 WARN 15768 --- [ctor-http-nio-4] .awreDefaultErrorWebExceptionHandler:無法處理請求[GET http:// localhost:8080 / values /?fiatCurrency = EUR] :響應狀態404
您尚未為/values
定義GET
映射。 您僅為POST
操作定義了它,請添加它,它應該可以工作。
嘗試更改此行
**<form th:action="@{/values/}" >
至
**<form th:action="@{/values/} + ${fiatCurrency}" method="post" >
這會將請求從“獲取”更改為“發布”(您現在在表單中具有“獲取”),並將信息作為pathvariable(在控制器方法中定義)發送,而不作為請求參數發送(如現在) )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.