Spring Cloud搭建微服务架构----使用Spring boot开发web项目
2017-04-19 00:00
666 查看
摘要: Spring生态是java web开发的最强大的生态,借助SpringBoot可以忽略之前使用SpringMVC时的配置过程,以一个更简单的方式去构建一个web应用,将更多的时间投入到业务处理中去。
项目服务实例之间主要通过RestAPI方式进行通信,所以服务本身可借助SpringBoot快速开发Restful web API。
获取响应:{"id":1,"content":"Hello, World!"}
使用工具:
IDEA;
Mven3;
HelloController.java
至此一个简单的Restful风格api构建成功,没有springmvc的xml文件需要配置,非常方便。
项目服务实例之间主要通过RestAPI方式进行通信,所以服务本身可借助SpringBoot快速开发Restful web API。
开发Restful web服务
以 http get方式访问:http://localhost:8080/hello获取响应:{"id":1,"content":"Hello, World!"}
使用工具:
IDEA;
Mven3;
实体类
Hello.javapackage hello; public class Hello{ private final long id; private final String content; public Hello(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }
构建Controller
通过注解@RestController 声明一个Restful接口。HelloController.java
@RestController public class HelloController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/hello") public Hellohello(@RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
入口Main文件
由于SpringBoot可以方便的通过jar文件进行交付,通过Main入口文件的配置可以启动一个内置的tomcat进行服务实例运行。@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
至此一个简单的Restful风格api构建成功,没有springmvc的xml文件需要配置,非常方便。
maven打包,执行jar包:
java -jar build/libs/gs-rest-service-0.1.0.jar访问http请求:
http://localhost:8080/greeting响应结果:
{"id":1,"content":"Hello, World!"}代码实例
https://github.com/zhangcj/easymall/tree/master/springbootdemo/springbootdemo-shoppingcart相关文章推荐
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- 使用Spring Boot开发Web项目
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Spring Boot(二):快速搭建web项目或微服务
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合 一、搭建开发环境 1.1、使用Maven创建Web项目 执行如下命令: mvn archetype:create
- 【备忘】使用dubbo、spring-boot等技术实现互联网后台服务项目架构视频教程
- 使用idea+springboot+Mybatis搭建web项目
- 使用Spring Boot开发Web项目
- 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程
- [置顶] WEB开发----Spring Boot项目快速搭建
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- 使用springboot进行web项目开发
- 使用idea开发工具,nginx服务部署Extjs6,SpringBoot项目到服务器
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- 使用Spring Boot开发Web项目