当前位置: 首页 > 知识库问答 >
问题:

Spring REST在POST请求上显示404错误

徐鸿文
2023-03-14

我得到的REST POST请求错误

>

  • 我使用的是H2 db,我就是这样配置的

    Springh2。安慰path=/h2 spring。h2。安慰启用=真正的Spring。数据源。驱动程序类名=org。h2。驱动Spring。数据源。url=jdbc:h2:文件:/db/daDbSpring。数据源。用户名=用户spring。数据源。密码=1234Spring。jpa。冬眠自动更新

    /////////////////////////////控制器////////////////////////////////////

    @RestController
    @RequestMapping(path = "/student",consumes = "application/json")
    public class StudentController
    {
        private final studentDAO studentDAO;
        @Autowired
        public StudentController()
        {
            this.studentDAO = new studentDAO();
        }
    
        //todo Add student subject(student id,subject id) fixed with patch/put mapping
        //todo Add Exception Handling
    
    
    
        @PostMapping(path = "/")
        @ResponseStatus(HttpStatus.CREATED)
        public void postRequest(@NonNull @RequestBody student student)
        {
            student.setId(UUID.randomUUID());
            this.studentDAO.add(student);
        }
    }
    

    //////////////////////////服务///////////////////////////

    @Service
    public class studentDAO extends daoTemplate<student,studentRepo>
    {
        @Autowired
        private studentRepo studentRepo;
    
        @Override
        public void add(student temp)
        {
            this.studentRepo.save(temp);
        }
    
        @Override
        public void patchUpdate(student student,UUID id)
        {
            Optional<student> target = super.getRepo().findById(id);
            if(target.isPresent())
            {
                if(Objects.nonNull(student.getName()))
                    target.get().setName(student.getName());
                if(Objects.nonNull(student.getSubjects()))
                    target.get().setSubjects(student.getSubjects());
            }
        }
    
    }
    

    DAO模板

    @Service
    public abstract class daoTemplate<entity,repo extends CrudRepository<entity,UUID>>
    {
        private repo repo;
    
        public repo getRepo()
        {
            return repo;
        }
    
        public entity get(UUID id)
        {
            Optional<entity> temp = this.repo.findById(id);
            if(temp.isPresent())
            {
                return temp.get();
            }
            return null;
        }
    
        public Iterable<entity> getAll()
        {
            return this.repo.findAll();
        }
    
        public void update(entity temp)
        {
            if (Objects.nonNull(temp))
                this.repo.save(temp);
        }
    
        public void patchUpdate(entity temp,UUID id)
        {
            //Manually Written
        }
    
        public void delete(UUID id)
        {
            if(this.repo.existsById(id))
                this.repo.deleteById(id);
        }
    
        public void add(entity temp)
        {
            this.repo.save(temp);
        }
    
        public boolean check(UUID id)
        {
            return this.repo.existsById(id);
        }
    }
    

    ////////////////////////////学生实体///////////////////////////////

    @Entity
    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class student
    {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private UUID id;
    
        @NotBlank
        @NotEmpty
        private String name;
    
        @NonNull
        private int age;
        
        @ManyToMany(mappedBy = "students",cascade = {CascadeType.MERGE,CascadeType.DETACH,
        CascadeType.PERSIST,CascadeType.REFRESH},fetch = FetchType.EAGER)
        Collection<subject>subjects;
    
        public void addSubjectByID(Collection<UUID> subjectsIDs)
        {
            if(!subjectsIDs.isEmpty())
            {
                subjectDAO dao = new subjectDAO();
                subjectsIDs.stream().distinct()
                        .map((id) -> {
                            return dao.get(id);
                        })
                        .forEach((subject) -> {
                            this.subjects.add(subject);
                        });
            }
        }
    
    }
    
  • 共有1个答案

    房项禹
    2023-03-14

    在post方法中,您正在传递两个学生对象:

    public void postRequest(@NonNull @RequestBody student student)
    

    应该是:

    public void postRequest(@NonNull @RequestBody Student student)
    

    此外,我建议您不要在post方法的路径中添加任何内容,因此,不要:

    @PostMapping(path = "/")
    

    只需使用:

    @PostMapping
    
     类似资料:
    • 我正在使用Spring4开发一个RESTful应用程序。我想处理当一个POST请求不包含正文时的情况。我编写了以下自定义异常处理程序: 当它收到一个没有正文的POST时,这些方法不会被调用。相反,客户端得到一个带有400个坏请求HTTP状态和空正文的响应。有人知道怎么处理吗?

    • 我已经做了一个简单的场景登录在ASP点网络应用程序。但是我在登录时发送的请求在执行时显示为GET。第一个图像是HTTP请求的屏幕截图,第二个图像是执行时请求详细信息的屏幕截图。如何将该请求转换为POST请求?还添加了显示“对象移动到这里”消息的响应数据截图。

    • 我正在Apache7.xx应用程序上开发Spring MVC,并且没有任何错误地设置了所有内容。 我有一个应用程序将我的dispatcher servlet映射到HomeController,HomeController为视图“home/view”提供服务,该视图也在工作。 spring-servlet相关片段(在我的例子中是cmgr-servlet) applicationcontext.xml

    • 我正在为一个项目使用Hackerrank API。查看官方文档,点击这里! 在他们的网站上有一个使用UNIREST的例子, 由于我使用的是axios,所以我将其转换为类似的axios代码,如下所示: 我希望这只适用于示例中所示的示例,但它给我带来了以下错误: 请求失败,状态代码为400 错误:请求失败,状态代码为400 在createError(createError.js:16) 在sett(s

    • 问题内容: 我刚刚更新了Angular + Ionic的新版本,并且处理远程请求的方法停止工作并始终返回404响应。 请求如下: 处理远程请求的方法的代码如下: 但是没有运气。 服务器端通过这种方式处理请求: 如果我试图使用邮递员或卷曲发送请求,一切似乎都在工作。 离子信息: AngularJS版本: 我该如何解决? 非常感谢您的任何建议 问题答案: 哼,我只是遇到了同样的问题:标头表明它已经被拿