resttemplate

Spring Boot End Point that generates a string response and displaying that response in a Angular Application

不羁岁月 提交于 2020-01-25 06:55:09
问题 I hope this isn't a repost, as I did look but couldn't find. I have a spring boot rest client that makes a call to a rest endpoint. I am using angular to consume the result of that call and pass it to the html file belonging to the component. My Spring Boot code looks like this: package com.terida.teridaapp; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework

Https2

时光总嘲笑我的痴心妄想 提交于 2020-01-25 03:08:04
package com.tojoy.wechatintegration.utils; import com.google.common.base.Stopwatch; import com.tojoy.wechatintegration.base.OgLog; import com.tojoy.wechatintegration.base.OptType; import lombok.Cleanup; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpEntity; import org.springframework.http

SpringCloud 服务消费者(rest+ribbon)报错  java.net.UnknownHostException

守給你的承諾、 提交于 2020-01-25 03:01:03
版本信息:springboot 2.0.3 springcloud Finchley.RELEASE @Service public class RibbonService { @Autowired @Qualifier(value = "restTemplate") private RestTemplate restTemplate; public String callEurekaClient(String name){ http://SERVICE-HI/hi?name= return restTemplate.getForObject("http://SERVICE-HI/eurekaClient/hi?name=" + name,String.class); } } 本地测试报错,报错信息为: java.net.UnknownHostException service-hi 解决方法: 检查springboot和springcloud版本是否匹配。 RestTemplate 需要加 @LoadBalanced @Configuration public class RibbonConfig { @Bean("restTemplate") @LoadBalanced public RestTemplate restTemplate(){ return new

Rest微服务案例(二)

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 01:05:42
1. 创建父工程 Maven Project 新建父工程microservicecloud,packaging是pom模式,pom.xml内容如下: <!-- SpringBoot父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath /> </parent> <properties> <!-- jdk version --> <java.version>1.8</java.version> <!-- springcloud version --> <cloud.version>Finchley.RELEASE</cloud.version> <!-- mysql version --> <mysql.version>5.1.48</mysql.version> <!-- druid version --> <druid.version>1.1.6</druid.version> <!-- junit version --> <junit.version>4.12</junit.version> <!-- log4j

SpringCloud(1)基本介绍以及demo

淺唱寂寞╮ 提交于 2020-01-24 19:35:25
目录 一 基本介绍 1.1 和SpringBoot是什么关系 1.2 和Dubbo的对比 1.3 资料 二 hello world 2.1 结构 2.2 microservicecloud 2.3 microservicecloud-api 2.4 microservicecloud-provider-dept-8001 配置文件 dao层 service controller 启动类 2.5 microservicecloud-consumer-dept-80 配置类 controller层 启动类 配置文件 测试地址 参考 一 基本介绍 SpringCloud,基于SpringBoot提供了 一套 微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器 等组件 ,除了基于NetFlix的开源组件做高度抽象封装之外,还有一些选型中立的开源组件。 SpringBoot并 没有重复 制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架 组合 起来,通过SpringBoot风格进行再封 装屏蔽掉了复杂的配置 和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包 1.1 和SpringBoot是什么关系 SpringBoot专注于快速方便的开发单个个体微服务。 SpringCloud是

How to send a multipart file to a service from another service

こ雲淡風輕ζ 提交于 2020-01-23 13:02:40
问题 I have two endpoints api which are /upload and /redirect /upload is where I directly upload the file. /redirect is where i receive the file and pass it to upload and get the JSON response from /upload .So below is my code: package com.example; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation

How to send a multipart file to a service from another service

[亡魂溺海] 提交于 2020-01-23 13:02:17
问题 I have two endpoints api which are /upload and /redirect /upload is where I directly upload the file. /redirect is where i receive the file and pass it to upload and get the JSON response from /upload .So below is my code: package com.example; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation

Spring Cloud Ribbon

左心房为你撑大大i 提交于 2020-01-22 22:25:47
目录 简介 使用Ribbon构建服务消费者 简介 Ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。 使用Ribbon构建服务消费者 新建 boot 项目时,选择: pom.xml 文件内容: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId

java8 CompletableFuture_异步执行多个任务

微笑、不失礼 提交于 2020-01-21 19:49:55
前言 CompletableFuture是java8提供的基于异步操作的封装,日常开发中怎能不用上一番呢。 1)聚合多个异步任务 需求:多个tab页包含不同的表格数据,并且带分页,页面首次加载时需要显示第一页数据,并且在tab顶上显示总量,如下图所示: 各个表格数据从es中查询,涉及到计算,此处可以让前端调用多个接口来组装数据,但考虑到查询效率和网络交互,采用后端以多线程的形式查询,组合多个结果返回给前端,后端实现如下: //所有订单 CompletableFuture<OrdersBo.Item> allOrdersFuture = fillAllOrders(pd, ordersBo); //及时量订单 CompletableFuture<OrdersBo.Item> inTimeOrdersFuture = fillInTimeOrders(pd, ordersBo); //超时量订单 CompletableFuture<OrdersBo.Item> timeOutFuture = fillOverTimeOrders(pd, ordersBo); //失败量 CompletableFuture<OrdersBo.Item> failOrderFutrue = fillFailOrders(pd, ordersBo); //异常量 CompletableFuture

通过 RestTemplate 访问 Web 资源

感情迁移 提交于 2020-01-21 02:05:46
Spring Boot 中的 RestTemplate 在springboot中,有两种方式去配置: 自己new 新建一个RestTemplate实例作为bean 放到容器当中 springboot 给我们提供了一个RestTemplateBuilder,通过RestTemplateBuilder.build()去获得一个RestTemplate RestTemplate中的常用方法: GET请求: getForObject() / getForEntity() POST请求: postForObject() /postForEntity() PUT请求: put() DELETE请求: delete() 构造 URI UriComponentsBuilder 构造 URI ServletUriComponentsBuilder 构造相对于当前请求的 URI MvcUriComponentsBuilder 构造指向 Controller 的 URI 常用构造方式: URI uri = UriComponentsBuilder . fromUriString ( "http://localhost:8080/hotels/{hotel}?q={q}" ) . build ( "west" , "123" ) ; URI uri = UriComponentsBuilder .