Reputation: 107
I am working on a full-stack application which contains a user, product, addon (toppings), and more.
However, when I try to run localhost:8080/Admin/users/Sean (example), it is supposed to promote the user. However, instead it simply gives me the above error. "Get not supported". This is happening despite the fact that I'm using putmapping, not getmapping. Below is provided code.
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>
If more code is needed please let me know, it is a fairly large application. Thank you.
Edit: By the way, I get the same error when I try to invoke the DeleteMappings as well.
Upvotes: 0
Views: 938
Reputation: 191748
Browsers don't run PUT commands if you just type in the address as the url. You'll need to use an HTTP client such as cURL or Postman
Upvotes: 2
Reputation: 1052
You request will not be answered by
@PutMapping("/users/{id}")
as it will only accept PUTs.
You need another endpoint for returning the html page (via the browsers GET request), like
@GetMapping("/users/{id}")
public ModelAndView showUserPage(User user){
return new ModelAndView("users","User", user);
}
Upvotes: 0
Reputation: 536
I believe that you should try /api/v1/
in front of your path meaning
localhost:8080/Admin/users/Sean
will become localhost:8080/api/v1/Admin/users/Sean
. Could you please try that ?
Upvotes: 0