java - 已解决 [org.springframework.web.HttpRequestMethodNotSupportedException : Request method 'POST' not supported]

标签 java spring rest thymeleaf

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

用户

 @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<>();

管理 Controller

   @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

最佳答案

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

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

user后面不应该有}

关于java - 已解决 [org.springframework.web.HttpRequestMethodNotSupportedException : Request method 'POST' not supported],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60036251/

相关文章:

java - 启动画面中的线程

java - Spring MVC 3.0 : how do I define an interceptor with annotations?

java - REST 客户端 restTemplate 无法获取对象集合

Java:使用class in读取文件。为什么没有输出?

java - 如何根据用户在数据库中设置的数据和时间来安排邮件或任何任务

java - 从已部署的 Web 应用程序获取 Tomcats webapps 目录中文件的相对路径

rest - 同一基于 Tomcat 的应用程序中的 Web 套接字和 Rest API

java - Spring JPA 创建具有空值的实体

java - Spring HATEOAS ControllerLinkBuilder 方法显着增加响应时间

c# - 身份验证失败时的 ServiceStack 自定义响应