405 - request GET and POST

user11042713 :

Hello i have problem with update object, i dont know how always aftre update data i have message: Request method 'GET' not supported. But date after refresh object is update.

Controller with GET and POST method to update object

@Controller
@RequestMapping("/packet")
public class PacketController {



    @GetMapping("/modify/{id}")
    public String modifyPacketGet(Model model, @PathVariable Long id)
    {
        model.addAttribute("channels", channelService.getAllChannels());
        model.addAttribute("packet", packetService.getById(id));
        return "packet/modify";
    }


    @PostMapping("/modify")
    public String modifyPacketPost(Model model, @ModelAttribute PacketDto packetDto)
    {
        packetService.updatePacket(packetDto);
        return "redirect:/packet/modify";
    }

HTML form

        <form th:action="@{/packet/modify}" method="post" th:object="${packet}" enctype="multipart/form-data">
            <input type="text" hidden="hidden" readonly="readonly" th:field="*{id}" />
            <input type="text" hidden="hidden" readonly="readonly" th:field="*{filename}" />
            <div class="form-group">
                <label for="name" class="h3 text-success">Name:</label>
                <input id="name" type="text" th:field="*{name}" class="form-control">
            </div>
            <div class="form-group">
                <label for="price" class="h3 text-success">Price:</label>
                <input id="price" type="text" th:field="*{price}" class="form-control">
            </div>
            <div class="form-group">
                <label for="description" class="h3 text-success">Description:</label>
                <textarea class="form-control" rows="5" th:field="*{description}" id="description"></textarea>
            </div>
            <div class="form-group">
                <label for="image" class="h3 text-success">Image:</label>
                <input id="image" type="file" th:field="*{multipartFile}"  accept="image/**" class="form-control">
            </div>
            <div class="form-group">
                <label for="channel" class="h2 text-secondary">Channels:</label>
                <ul class="list-inline">
                    <li  class="list-inline-item" th:each="c : ${channels}">
                        <input id="channel" type="checkbox" th:field="*{channelIds}" th:value="${c.id}">
                        <label th:text="${c.name}"></label>
                    </li>
                </ul>
            </div>

                <button type="submit" class="btn btn-success btn-lg mr-2">Add</button>
        </form>
Marco R. :

The http request GET /packet/modify is not being handled in your controller and you are redirecting your POST method to that http request:

        return "redirect:/packet/modify";

To solve this you need to do one of the following:

  1. Change the redirect request in your POST to an endpoint that is being handled:

        return "redirect:/packet/modify/" + packetDto.getPacketId();
    
  2. Or, handle that GET endpoint:

        @GetMapping("/modify/")
        public String retrievePacket(...) { ... }
    

Hope this helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93299&siteId=1