使用spring的restelplate的post方法,向web程序发起请求,参数用各种变量,包括@RequestBody。
@Controller @RequestMapping("/task") public class TaskWithTokenController extends BaseController { @Autowired private DataVersionService dataVersionService; @Autowired private TaskService taskService; @Autowired private SysService sysService; @RequestMapping(value = "/{token}/{userId}/update", method = RequestMethod.POST) public void update(HttpServletResponse response, @RequestBody Task task, @PathVariable("token") String token, @PathVariable("userId") String userId) { LOGGER.debug("update"); try { long taskSysId = checkTokenForTask(task.getTaskId(), token); task.setSysId(taskSysId); taskService.updateTaskAndAddLog(task, userId); processSuccessForResponse(response); } catch (Exception e) { LOGGER.error(e.getMessage(), e); processErrorForResponse(response, e.getMessage()); } } private long checkTokenForTask(long taskId, String token) throws Exception { Task t = taskService.query(taskId); Preconditions.checkNotNull(t, "不存在任务ID是:[%s] 的任务! "); long taskSysId = t.getSysId().longValue(); List<Sys> syses = sysService.queryAllOnlineSyss(); String taskToken = null; for (Sys sys : syses) { if (taskSysId == sys.getSysId().longValue()) { taskToken = sys.getToken(); break; } } String err = String.format("%s 是一个无效的token.", token); Preconditions.checkArgument(StringUtils.isNotBlank(taskToken), err); Preconditions.checkArgument(token.equals(taskToken), err); return taskSysId; } }
@RequestMapping(value = "/updateTask", method = { RequestMethod.POST }) @ResponseBody public AjaxResult updateTask(@RequestBody Task task, @CookieValue(Consts.COOKIE_USER_KEY) String userId) { LOGGER.info("user {} start update task {}", userId, task); // 检验字段格式 if ((task.getTaskId() == null) || (task.getTaskId() == -1)) { return AjaxResult.errorMessage("taskId is wrong!"); } String url = "http://localhost:8080/dmap-apiserver/api/task/{token}/{userId}/update"; try { defaultTaskValue(task); Map<String, String> uriVariables = Maps.newHashMap(); uriVariables.put("token", token); uriVariables.put("userId", userId); ResponseEntity<String> rs = restTemplate.postForEntity(url, task, String.class, uriVariables); String body = rs.getBody(); AjaxResult ars = JSON.parseObject(body, AjaxResult.class); return ars; } catch (Exception e) { LOGGER.error(e.getMessage(), e); return AjaxResult.errorMessage(e.getMessage()); } }
相关推荐
how to use spring validator
How to use projector
How To Use Adobe Photoshop CS2 <br>Photoshop CS2使用指南 <br>By Daniel Giordan, Doug Nelson <br>Publisher: Sams Publishing <br>Have you ever looked at your color pictures and wished ...
How to Use Objects Code and Concepts Holger Gast
How to use epoll A complete example in C How to use epoll A complete example in C How to use epoll A complete example in C How to use epoll A complete example in C
this article introduces how to use awk in linux
How to use the Bayes Net Toolbox? This documentation was last updated on 29 October 2007.
packaging and running portable distributed applications, Deepak Vorhadiscusses how to build, ship and run applications on any platform such as a PC, the cloud, data center or a virtual machine....
Simple application that shows how to use the Data Control to connect to the Biblio.mdb database and display all authors in the Authors table.
After reading this book, you’ll be able to use Spring Boot to simplify the development of your own Spring projects, as well as take advantage of Spring Cloud Task and Spring Cloud Data Flow for ...
You will learn how to use Spring RestTemplate, Ribbon, and Feign clients with or without service discovery. Chapter 7, Advanced Load Balancing and Circuit Breakers, will described more advanced ...
What you'll learn Key Spring Framework fundamentals How to use the ...how to use the Spring Web Flow framework How to test your Spring MVC applications How to implement Spring Security Who this book is ...
IBM How to use Seven Keys To Success.
Interesting, entertaining, and challenging exercises encourage students to make a difference and use computers and the Internet to work on problems. To keep readers up-to-date with leading-edge ...
He explains the roles of individual objects in a complete application, how they react to events and fulfill service requests, and how to transform excellent designs into excellent code. Using ...