How To Use Spring RESTTemplate To Post Data to a Web Service

使用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());
        }

    }

参考: http://johnathanmarksmith.com/spring/java/javaconfig/programming/spring%20java%20configuration/spring%20mvc/web/rest/resttemplate/2013/06/18/how-to-use-spring-resttemplate-to-post-data-to-a-web-service/

猜你喜欢

转载自kanpiaoxue.iteye.com/blog/2213266
今日推荐