出现意外错误(类型=方法不允许,状态=405)。不支持请求方法'GET',但我正在使用放置映射



我正在开发一个包含用户、产品、插件(浇头)等的全栈应用程序。

然而,当我尝试运行localhost:8080/Admin/users/Sean(示例)时,它应该提升用户。然而,它只是给了我上面的错误。"得不到支持"。尽管我使用的是putmapping,而不是getmapping,但还是发生了这种情况。下面是提供的代码:

AdminController.java

package com.example.pizza_order_service.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.pizza_order_service.model.Addon;
import com.example.pizza_order_service.model.Product;
import com.example.pizza_order_service.model.User;
import com.example.pizza_order_service.service.AddonService;
import com.example.pizza_order_service.service.ProductService;
import com.example.pizza_order_service.service.UserService;

@Controller
@RequestMapping("/Admin")
public class AdminController {
@Autowired
private ProductService prodService;

@Autowired
private UserService userService;

@Autowired
private AddonService addonService;

@GetMapping("")
public ModelAndView listProducts() {
List<Product> products = prodService.findAll();
return new ModelAndView("product","Product", products);
}

@GetMapping("/")
public ModelAndView listAddons() {
List<Addon> addons = addonService.findAll();
return new ModelAndView("product","Addon", addons);
}

@GetMapping("/Product")
public String displayProducts(ModelMap model) {
Iterable<Product> results = prodService.findAll();
model.addAttribute("product", results);
return "product";
}

@GetMapping("/Toppings")
public String displayToppings(ModelMap model) {
Iterable<Addon> results = addonService.findAll();
model.addAttribute("toppings", results);
return "Addon";
}


// - Promote Users

@PutMapping("/users/{id}")
public String promoteUser(User newUser){
User user = userService.findUserByUserName(newUser.getName());
user.setRole(newUser.getRole());
return "Promotion confirmed";
}


//- Delete Product

@DeleteMapping(value="/deleteProd/{id}")
public ModelAndView deleteAProduct(@PathVariable("id") long id)  {  
prodService.deleteProduct(id);
return listProducts();
}

//- Delete Toppings

@DeleteMapping(value="/deleteAddon/{id}")
public ModelAndView deleteAnAddon(@PathVariable("id") long id)  {  
addonService.deleteAddon(id);
return listAddons();
}


}

users.html

<!-- HTML -->
<html id="content">
<head>
<div th:replace="fragment.html :: header"></div>
<!-- CSS -->
<style>
td {
padding: 10px;
}
</style>
<!-- End CSS -->
</head>
<body class="background" style="overflow: hidden;">
<!-- Navbar -->
<div th:replace="fragment.html :: navbar"></div>
<!-- Menu -->
<br><br><br><br><br><br><br>
<table class="xy-center" style="position: fixed; background-color: var(--tertiary); color: var(--primary); font-size: 3.5vh; border-radius: 15px; padding: 5px;">
<b th:utext="${user}"></b>
</table>
<!-- End Menu -->
<p>
<br>
<br>
<br><br><br><br><br><br>
Hello World!</p>

</body>
<!-- JS -->
<script>
// To open the navbar buttons
$(document).ready(function() {
$('.first-button').on('click', function () {
$('.animated-icon1').toggleClass('open');
});
});
</script>
<!-- End JS -->
</html>

如果需要更多的代码请让我知道,这是一个相当大的应用程序。谢谢你。

编辑:顺便说一下,当我试图调用deletemapping时,我也会得到同样的错误。

如果你只输入地址作为url,浏览器不会运行PUT命令。您需要使用HTTP客户端,如cURL或Postman

我认为你应该在你的路径意义前尝试/api/v1/localhost:8080/Admin/users/Sean变成localhost:8080/api/v1/Admin/users/Sean。你能试试吗?

您的请求将无法得到响应

@PutMapping("/users/{id}")

,因为它只接受put。

您需要另一个端点来返回html页面(通过浏览器GET请求),如

@GetMapping("/users/{id}")
public ModelAndView showUserPage(User user){
return new ModelAndView("users","User", user);
}