Spring Boot完整的REST CRUD示例

Jos*_*eon 7 spring spring-boot

有没有人有一个完整的春季启动REST CRUD示例?spring.io网站只有一个RequestMapping for GET.我能够使POST和DELETE工作,但不能PUT.

我怀疑这是我试图获得断开连接的参数,但我还没有看到有人正在执行更新的示例.

我目前正在使用SO iPhone应用程序,因此无法粘贴当前代码.任何有效的例子都会很棒!

Edd*_*dez 10

如您所见,我已经实现了两种更新方式.第一个将收到一个json,第二个将收到URL和json中的cusotmerId.

@RestController
@RequestMapping("/customer")
public class CustomerController {


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Customer greetings(@PathVariable("id") Long id) {
        Customer customer = new Customer();
        customer.setName("Eddu");
        customer.setLastname("Melendez");
        return customer;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<Customer> list() {
        return Collections.emptyList();
    }

    @RequestMapping(method = RequestMethod.POST)
    public void add(@RequestBody Customer customer) {

    }

    @RequestMapping(method = RequestMethod.PUT)
    public void update(@RequestBody Customer customer) {

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public void updateById(@PathVariable("id") Long id, @RequestBody Customer customer) {

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete() {

    }

    class Customer implements Serializable {

        private String name;

        private String lastname;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public String getLastname() {
            return lastname;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

或者,或者:

@RepositoryRestResource
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
Run Code Online (Sandbox Code Playgroud)

要使用注释org.springframework.data.rest.core.annotation.RepositoryRestResource,需要将以下依赖项添加到pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)