Tag: resttemplate

RestTemplate应该是全局声明的静态吗?

我在我的代码中使用Java Callable Future。 下面是我使用未来和callables的主要代码 – public class TimeoutThread { public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newFixedThreadPool(5); Future future = executor.submit(new Task()); try { System.out.println(“Started..”); System.out.println(future.get(3, TimeUnit.SECONDS)); System.out.println(“Finished!”); } catch (TimeoutException e) { System.out.println(“Terminated!”); } executor.shutdownNow(); } } 下面是我的Task类,它实现了Callable接口,我需要根据我们拥有的主机名生成URL,然后使用RestTemplate调用SERVERS。 如果第一个主机名中有任何exception,那么我将为另一个主机名生成URL,我将尝试拨打电话。 class Task implements Callable { private static RestTemplate restTemplate = new […]

使用Spring Rest模板+ Spring Web MVC进行多部分文件上载

我试图使用RestTemplate上传一个文件与以下代码。 MultiValueMap multipartMap = new LinkedMultiValueMap(); multipartMap.add(“file”, new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType(“multipart”, “form-data”)); HttpEntity<MultiValueMap> request = new HttpEntity<MultiValueMap>(multipartMap, headers); System.out.println(“Request for File Upload : ” + request); ResponseEntity result = template.get().exchange( contextPath.get() + path, HttpMethod.POST, request, byte[].class); 我有MultipartResolver bean和Controller代码 @RequestMapping(value = “/{id}/image”, method = RequestMethod.POST) @ResponseStatus(HttpStatus.NO_CONTENT) @Transactional(rollbackFor = Exception.class) public byte[] […]