【问题标题】:Patch in SpringDataJPASpring Data JPA 中的补丁
【发布时间】:2017-11-26 13:15:57
【问题描述】:

我正在用 Android 编写一个移动应用程序。我有一个 TomCat 服务器,使用 SpringBoot 和 SpringData JPA。要将 Android 连接到服务器,我使用 Retrofit。我是新手,所以你能帮我看看如何使用补丁更新数据库吗?我有一个控制器:

@RestController
@RequestMapping("/student") 
public class StudentController {
private StudentRepository studentRepository;

@Autowired
public StudentController(StudentRepository studentRepository) {
    this.studentRepository = studentRepository;
}



@RequestMapping(method = RequestMethod.PATCH)//dodanie
public String updateStudent(@RequestBody Student student)
{
   studentRepository.save(student);
    return "{success:true}";
}

//zwraca liste studentow
@RequestMapping(method = RequestMethod.GET)
public List<Student> getStudent(){
    return studentRepository.findAll();
}
}

JPA 存储库:

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long>{

}

安卓系统:

public interface StudentClient {
@PATCH("student")

Call<Student>updateStudent(@Body Student student);
 }

有什么遗漏吗?我应该在任何地方或 JPA 中添加一些东西吗?

【问题讨论】:

  • 我们不知道是否缺少任何东西。你告诉我们。你有什么错误吗?有什么不符合您的预期吗?
  • 为什么要使用PATCH,添加/更新应该使用POST/PUT

标签: java android spring spring-data-jpa retrofit


【解决方案1】:

你的问题很笼统。

您可以参考本教程来了解如何在服务器端实现 PATCH 动词。

http://www.baeldung.com/http-put-patch-difference-spring

还有这个 UI 方面的教程。

https://futurestud.io/tutorials/retrofit-2-how-to-update-objects-on-the-server-put-vs-patch

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 2017-11-29
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2020-01-27
    • 2019-05-17
    相关资源
    最近更新 更多