spring boot http调用其他服务并解析
2017-09-14 22:34
615 查看
新建一个maven项目
pom和 上一个一样就行了,注意properties里面多一个
#tomcat
server.port=8081
这样我就直接把他端口改了--------瞬间整儿人就好了----
package ccy.consumer_movie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@SpringBootApplication
public class StartMain {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(StartMain.class, args);
}
}
----------@Bean 是创建一个bean 相当于以前的配置文件 <bean> RestTemplate 这货是发送http的 整体感觉有点类似hibernate以前的JdbcTemplate注入,也是通过配置直接打到属性的set方法上面 ----------------------
package ccy.consumer_movie.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import ccy.consumer_movie.model.User;
@RestController
@RequestMapping("movieController")
public class MovieController {
@Autowired
private RestTemplate restTemplate;
/**
* http://localhost:8081/movieController/findById?id=1 * @param id
* @return
*/
@RequestMapping(value = "findById")
public User findById(Long id) {
System.out.println("--movieController-接到id--" + id);
String url="http://localhost:8080/userController/findById?id="+id;
User user=restTemplate.getForObject(url, User.class);
return user;
}
}
-----------------这里面的RestTemplate 就是你刚才搞进去的,从IOC上取得,这里要么理解一下Spring的IOC,简单来说就是个map,要么就呵呵了,反正可以-----------------------
User user=restTemplate.getForObject(url, User.class); 这句话有很大的信息量,
1.上一个项目会给你个json
1.返回的是个json能够帮你自动映射出来
User类和上一个一模一样就不说了
-------------------------------------------------------
通过(二) 和(三) 也就能够 搭建简单的SpringBoot以及对外访问,对数据库访问 和 提供访问资源了
后续开始集成springCloudy
pom和 上一个一样就行了,注意properties里面多一个
#tomcat
server.port=8081
这样我就直接把他端口改了--------瞬间整儿人就好了----
package ccy.consumer_movie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@SpringBootApplication
public class StartMain {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(StartMain.class, args);
}
}
----------@Bean 是创建一个bean 相当于以前的配置文件 <bean> RestTemplate 这货是发送http的 整体感觉有点类似hibernate以前的JdbcTemplate注入,也是通过配置直接打到属性的set方法上面 ----------------------
package ccy.consumer_movie.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import ccy.consumer_movie.model.User;
@RestController
@RequestMapping("movieController")
public class MovieController {
@Autowired
private RestTemplate restTemplate;
/**
* http://localhost:8081/movieController/findById?id=1 * @param id
* @return
*/
@RequestMapping(value = "findById")
public User findById(Long id) {
System.out.println("--movieController-接到id--" + id);
String url="http://localhost:8080/userController/findById?id="+id;
User user=restTemplate.getForObject(url, User.class);
return user;
}
}
-----------------这里面的RestTemplate 就是你刚才搞进去的,从IOC上取得,这里要么理解一下Spring的IOC,简单来说就是个map,要么就呵呵了,反正可以-----------------------
User user=restTemplate.getForObject(url, User.class); 这句话有很大的信息量,
1.上一个项目会给你个json
1.返回的是个json能够帮你自动映射出来
User类和上一个一模一样就不说了
-------------------------------------------------------
通过(二) 和(三) 也就能够 搭建简单的SpringBoot以及对外访问,对数据库访问 和 提供访问资源了
后续开始集成springCloudy
相关文章推荐
- Spring Boot 中使用Feign作为HTTP客户端调用远程HTTP服务
- SpringBoot发布HttpClient服务和客户端调用HttpClient服务
- Spring Boot 实践3 --基于spring cloud 实现微服务的简单调用
- Http服务与Dubbo服务相互转换的Spring Boot代理节点实现
- springboot下dubbo2.8.4服务的提供与消费(A模块调用B模块提供的服务并对外提供新的服务)
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,feign熔断器
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Boot整合spring-ws调用web service服务
- android界的spring-boot http服务框架
- Win7/Vista/Server2008下VS 环境 调试调用 HTTP.SYS 无法启动监听服务及启动后其他机器无法访问端口
- 如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https。
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Springboot 实现 Restful 服务,基于 HTTP / JSON 传输
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
- 使用Spring Boot发布服务和调用服务
- spring boot actuator工作原理之http服务暴露源码分析
- 远程服务调用之RMI、Hessian、Burlap、Spring的HTTPinvoker
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务