【问题标题】:Failed to handle request [GET http://localhost:8080 ]: Response status 404 Spring, RESTfull ,thymleaf无法处理请求 [GET http://localhost:8080]:响应状态 404 Spring,RESTfull,thymleaf
【发布时间】:2018-10-19 18:40:16
【问题描述】:

我遇到了 Spring、Rest 和 Thymeleaf 的问题,我被困在那里,关于我遇到的这个错误没有太多细节。

当我在表单中选择选择标记 (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

【问题讨论】:

    标签: java spring rest api thymeleaf


    【解决方案1】:

    尝试改变这一行

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

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

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

    【讨论】:

      【解决方案2】:

      根据您的错误堆栈跟踪,

      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

      您尚未为/values 定义GET 映射。您仅为 POST 操作定义了它,添加它,它应该可以工作。

      【讨论】:

      • 我添加了它,它解决了这个问题,我像这样添加它 @GetMapping(value = {"/values","/values/","", "/", "/index" ,"/cryptos"} ),现在我有一个新问题,它在任何地方都没有失败,但是当我在选择标签中选择另一个值时没有做任何事情。
      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 2020-02-20
      • 2020-06-20
      • 1970-01-01
      • 2021-08-28
      • 2019-05-08
      • 2021-05-08
      • 2021-05-23
      相关资源
      最近更新 更多