-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller_model_java.vm
116 lines (98 loc) · 4.88 KB
/
controller_model_java.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2019. All right reserved
* Created on $today.date ( Date ISO $today.date("yyyy-MM-dd") - Time $today.time )
* Generated by $generator.name ( version $generator.version )
*/
package ${target.javaPackageFromFolder(${SRC})};
import ${ROOT_PKG}.entities.${entity.name};
import ${ROOT_PKG}.service.${entity.name}Service;
#set( $uncapitalizedEntityName = $fn.uncapitalize($entity.name) )
#set( $controllerClassName = "${entity.name}Controller" )
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import java.util.List;
/**
* REST controller for managing {@link ${entity.name}}.
*
* @author @aek
*/
@RestController
@RequestMapping("/api/${uncapitalizedEntityName}")
public class $controllerClassName {
private final Logger log = LoggerFactory.getLogger(${controllerClassName}.class);
private final ${entity.name}Service entityService;
public $controllerClassName (${entity.name}Service entityService) {
this.entityService = entityService;
}
/**
* {@code POST /${uncapitalizedEntityName}} : Create a new ${uncapitalizedEntityName}.
*
* @param ${uncapitalizedEntityName} the ${uncapitalizedEntityName} to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new ${uncapitalizedEntityName}.
*/
@PostMapping()
public ResponseEntity<${entity.name}> create${entity.name}(@RequestBody @Valid ${entity.name} ${uncapitalizedEntityName}) {
log.debug("REST request to save ${entity.name} : {}", ${uncapitalizedEntityName});
return new ResponseEntity<>(entityService.create(${uncapitalizedEntityName}), HttpStatus.CREATED);
}
/**
* {@code PUT /${uncapitalizedEntityName}} : Updates an existing ${uncapitalizedEntityName}.
*
* @param ${uncapitalizedEntityName} the ${uncapitalizedEntityName} to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated ${uncapitalizedEntityName},
* or with status {@code 400 (Bad Request)} if the ${uncapitalizedEntityName} is not valid,
* or with status {@code 500 (Internal Server Error)} if the ${uncapitalizedEntityName} couldn't be updated.
*/
@PutMapping()
public ResponseEntity<${entity.name}> update${entity.name}(@Valid @RequestBody ${entity.name} ${uncapitalizedEntityName}) {
log.debug("REST request to update ${entity.name} : {}", ${uncapitalizedEntityName});
${entity.name} result = entityService.update(${uncapitalizedEntityName});
return ResponseEntity.ok().body(result);
}
/**
* {@code GET /${uncapitalizedEntityName}} : get all the ${uncapitalizedEntityName}s.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of ${uncapitalizedEntityName} in body.
*/
@GetMapping()
public ResponseEntity<List<${entity.name}>> getAll${entity.name}() {
log.debug("REST request to get all ${uncapitalizedEntityName}s");
List<${entity.name}> lst = entityService.getAll();
return new ResponseEntity<>(lst,HttpStatus.OK);
}
/**
* {@code GET /${uncapitalizedEntityName}/:id} : get the "id" ${uncapitalizedEntityName}.
*
* @param id the id of the ${uncapitalizedEntityName} to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the ${uncapitalizedEntityName}, or with status {@code 404 (Not Found)}.
*/
@GetMapping(value = "/{id}")
public ResponseEntity<${entity.name}> getOne${entity.name}(@PathVariable("id") ${entity.keyAttributes[0].formattedType(0)} id) {
log.debug("REST request to get ${entity.name} : {}", id);
${entity.name} e = entityService.getOne(id);
return new ResponseEntity<>(e, HttpStatus.OK);
}
/**
* {@code DELETE /${uncapitalizedEntityName}/:id} : delete the "id" ${uncapitalizedEntityName}.
*
* @param id the id of the ${uncapitalizedEntityName} to delete.
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete${entity.name}(@PathVariable("id") ${entity.keyAttributes[0].formattedType(0)} id) {
log.debug("REST request to delete ${entity.name} : {}", id);
entityService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}