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

标签 java spring rest api thymeleaf

我遇到了 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/

相关文章:

java - 访问对象数组中的 object.variable

java - 如何在 java 中获取 liberty server.xml 变量?

java - Spring Rest Controller 返回特定字段

java - 如何在发布请求中使用 jersey(jax-rs) 客户端将一个对象作为另一个对象的属性传递?

java - 向类添加静态方法并委托(delegate)给现有方法

java - 是否在可能的良好做法时将方法声明为静态?

java - 如何在java中打开xls文件,保存它,然后将其全部关闭?

java - 设计一个 RESTful 网站

与 Net Framework 2.0 兼容的 C# JSON Rest 客户端

java - 如何在 JAX-RS 中获取当前调度的 Web 资源的 URI?