window.ENTITIES={'/api/snippets/java/spring%20boot%202%20-%20controller%20example%20that%20implements%20crud%20using%20jpa':[{"result":true,"message":null,"batch":{"type":"java","name":"spring boot 2 - controller example that implements crud using jpa","items":[{"id":"Dlkv21","type":"java","name":"Spring Boot 2 - controller example that implements CRUD using JPA","content":"package com.example.demo;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.transaction.annotation.Transactional;\nimport org.springframework.web.bind.annotation.*;\n\nimport java.util.List;\nimport java.util.Optional;\n\n@Controller\npublic class UsersController {\n\n @Autowired\n private UsersRepository usersRepository;\n\n // http://localhost:8080/api/users\n //\n @GetMapping(\"/api/users\")\n @Transactional(readOnly = true)\n public ResponseEntity> getUsers() {\n return ResponseEntity.ok(this.usersRepository.findAll());\n }\n\n // http://localhost:8080/api/users/1\n //\n @GetMapping(\"/api/users/{id}\")\n @Transactional(readOnly = true)\n public ResponseEntity getUser(@PathVariable(\"id\") Long userId) {\n return ResponseEntity.of(this.usersRepository.findById(userId));\n }\n\n // echo '{\"name\":\"john\",\"email\":\"john@email.com\"}' | curl -X POST -H \"Content-Type: application/json\" -d @- http://localhost:8080/api/user/create\n //\n @PostMapping(\"/api/user/create\")\n @Transactional\n public ResponseEntity createUser(@RequestBody UserEntity userEntity) {\n return ResponseEntity.ok(this.usersRepository.save(userEntity));\n }\n\n // echo '{\"name\":\"chris\",\"email\":\"chris@email.com\"}' | curl -X POST -H \"Content-Type: application/json\" -d @- http://localhost:8080/api/users/1/update\n //\n @PostMapping(\"/api/users/{id}/update\")\n @Transactional\n public ResponseEntity updateUser( @PathVariable(\"id\") Long userId, @RequestBody UserEntity newUserEntity) {\n Optional foundUserOptional = this.usersRepository.findById(userId);\n if (foundUserOptional.isPresent()) {\n UserEntity foundUserEntity = foundUserOptional.get();\n foundUserEntity.setName(newUserEntity.getName());\n foundUserEntity.setEmail(newUserEntity.getEmail());\n this.usersRepository.save(foundUserEntity);\n }\n return ResponseEntity.of(foundUserOptional);\n }\n\n // http://localhost:8080/api/users/1/remove\n //\n @GetMapping(\"/api/users/{id}/remove\")\n @Transactional\n public ResponseEntity removeUser(@PathVariable(\"id\") Long userId) {\n Optional foundUserOptional = this.usersRepository.findById(userId);\n if (foundUserOptional.isPresent()) {\n this.usersRepository.deleteById(userId);\n }\n return ResponseEntity.of(foundUserOptional);\n }\n}","source":"https://dirask.com/posts/Spring-Boot-2-CRUD-example-using-JPA-and-MySQL-database-pORVZD","author":{"id":"Ro42dD","name":"Creg","avatar":"1667335677213__Ro42dD__w40px_h40px.jpg","points":9600,"role":"ADMIN"},"creationTime":1652578122000,"updateTime":1652578329000,"removalTime":null}]}}]};