【问题标题】:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法 'POST']
【发布时间】:2020-05-19 00:48:08
【问题描述】:

我有两个列表,一个包含用户,另一个包含团队。我可以从列表中选择任何用户以及任何团队。 但无法将用户添加到团队。 当您按下按钮时,错误已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

用户

 @Entity
    @Table(name="users")
    public class Users {
        @Id
        @Column(name="email",/*unique = true,*/ nullable = false,length = 200)
        String email;         
        @Column(name="name",nullable = false,length = 200)
        String name;        
        @Column(name="password",nullable = false,length = 128)
        @JsonIgnore 
        String password;        
        @Column(name = "avatar", nullable = true)
        String avatar;            
        @ManyToOne
        @JoinColumn(name="team_id", nullable=true)
        Team team;       

团队

Entity
@Table(name="team")
public class Team  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    Long id;        
    @Column
    String name;    
    @Column
    String url;    
    @Lob
    @Column(name = "avatar",nullable = true,columnDefinition="BLOB")
    String avatar;    
    @OneToMany(mappedBy="team",cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnore
    Set<Users> users = new HashSet<>();

管理员控制器

   @RequestMapping(value = "/admin/team/user}", method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public String addUserToTeam(@PathVariable String userName, @PathVariable String teamName,Model model, @ModelAttribute("userTeamForm") @Validated UserTeamForm userTeamForm,
            BindingResult result, final RedirectAttributes redirectAttributes) {
        Team team = teamRepository.findTeamByName(teamName).orElseThrow(() -> new NoSuchTeamException("Team not found"));
        Users user = userRpRepository.findUsersByName(userName)
                                     .orElseThrow(() -> new NoSuchUserException("User not found"));
        user.setTeam(team);
        userRpRepository.save(user);
        return "userTeam";
    }   
  @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String adminPage(Model model) {
        model.addAttribute("userTeamForm",new UserTeamForm());
     ....
        return "admin";
    }   

admin.html

  <form th:action="@{/admin/team/user/}"th:object="${userTeamForm}" method="POST">
                   <div class="form-group blu-margin">
                       <select class="form-control" id="addUser">
                           <option value="0">select user</option>
                           <option th:each="user : ${users}" th:value="${user.name}" th:text="${user.name}"></option>
                       </select>
                       <select class="form-control" id="addTeam">
                           <option value="0">select team</option>
                           <option th:each="team : ${teams}" th:value="${team.name}" th:text="${team.name}"></option>
                       </select>
                   </div>
                   <br/>
                   <input type="submit" value="Add User to Team" />
               </form>

用户团队表单

我需要使用 UserTeamForm 还是可以直接使用 Entity?

public class UserTeamForm {
            private String userName;        
        private String teamName;
    get/set

【问题讨论】:

  • 请分享完整的堆栈跟踪。
  • 事实上这是整个堆栈。当您按下按钮时,错误已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

标签: java spring rest thymeleaf


【解决方案1】:

对不起,我没有注意到方法描述中的 }。 找了两天的问题))

@RequestMapping(value = "/admin/team/user}", method = RequestMethod.POST,

用户后面不应该有}

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 2020-10-29
    • 2021-09-20
    • 2020-12-23
    • 2022-01-06
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多