forked from springside/springside4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
18 changed files
with
430 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 97 additions & 97 deletions
194
...chetype/src/main/resources/archetype-resources/src/main/java/rest/TaskRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,100 @@ | ||
#set( $symbol_pound = '#' ) | ||
#set( $symbol_dollar = '$' ) | ||
#set( $symbol_escape = '\' ) | ||
/******************************************************************************* | ||
* Copyright (c) 2005, 2014 springside.github.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
*******************************************************************************/ | ||
package ${package}.rest; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import javax.validation.Validator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import ${package}.entity.Task; | ||
import ${package}.service.task.TaskService; | ||
import org.springside.modules.beanvalidator.BeanValidators; | ||
import org.springside.modules.web.MediaTypes; | ||
|
||
/** | ||
* Task的Restful API的Controller. | ||
* | ||
* @author calvin | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/api/v1/task") | ||
public class TaskRestController { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(TaskRestController.class); | ||
|
||
@Autowired | ||
private TaskService taskService; | ||
|
||
@Autowired | ||
private Validator validator; | ||
|
||
@RequestMapping(method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public List<Task> list() { | ||
return taskService.getAllTask(); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public Task get(@PathVariable("id") Long id) { | ||
Task task = taskService.getTask(id); | ||
if (task == null) { | ||
String message = "任务不存在(id:" + id + ")"; | ||
logger.warn(message); | ||
throw new RestException(HttpStatus.NOT_FOUND, message); | ||
} | ||
return task; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) | ||
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) { | ||
// 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. | ||
BeanValidators.validateWithException(validator, task); | ||
|
||
// 保存任务 | ||
taskService.saveTask(task); | ||
|
||
// 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. | ||
Long id = task.getId(); | ||
URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation(uri); | ||
|
||
return new ResponseEntity(headers, HttpStatus.CREATED); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON) | ||
public ResponseEntity<?> update(@RequestBody Task task) { | ||
// 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. | ||
BeanValidators.validateWithException(validator, task); | ||
// 保存 | ||
taskService.saveTask(task); | ||
|
||
// 按Restful约定,返回204状态码, 无内容. 也可以返回200状态码. | ||
return new ResponseEntity(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void delete(@PathVariable("id") Long id) { | ||
taskService.deleteTask(id); | ||
} | ||
} | ||
/******************************************************************************* | ||
* Copyright (c) 2005, 2014 springside.github.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
*******************************************************************************/ | ||
package ${package}.rest; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import javax.validation.Validator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import ${package}.entity.Task; | ||
import ${package}.service.task.TaskService; | ||
import org.springside.modules.beanvalidator.BeanValidators; | ||
import org.springside.modules.web.MediaTypes; | ||
|
||
/** | ||
* Task的Restful API的Controller. | ||
* | ||
* @author calvin | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/api/v1/task") | ||
public class TaskRestController { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(TaskRestController.class); | ||
|
||
@Autowired | ||
private TaskService taskService; | ||
|
||
@Autowired | ||
private Validator validator; | ||
|
||
@RequestMapping(method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public List<Task> list() { | ||
return taskService.getAllTask(); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public Task get(@PathVariable("id") Long id) { | ||
Task task = taskService.getTask(id); | ||
if (task == null) { | ||
String message = "任务不存在(id:" + id + ")"; | ||
logger.warn(message); | ||
throw new RestException(HttpStatus.NOT_FOUND, message); | ||
} | ||
return task; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) | ||
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) { | ||
// 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. | ||
BeanValidators.validateWithException(validator, task); | ||
|
||
// 保存任务 | ||
taskService.saveTask(task); | ||
|
||
// 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. | ||
Long id = task.getId(); | ||
URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation(uri); | ||
|
||
return new ResponseEntity(headers, HttpStatus.CREATED); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON) | ||
// 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码. | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void update(@RequestBody Task task) { | ||
// 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. | ||
BeanValidators.validateWithException(validator, task); | ||
|
||
// 保存任务 | ||
taskService.saveTask(task); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void delete(@PathVariable("id") Long id) { | ||
taskService.deleteTask(id); | ||
} | ||
} |
Oops, something went wrong.