Spring----Spring Boot Rest的使用方法
1.下载Google浏览器并安装插件
转载:
http://chromecj.com/web-development/2015-03/401/download.html
打开Google浏览器-》点击右上角-》更多工具-》扩展程序
然后把文件复制进来
打开右上角
完成
2.RestMain.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 测试RestFul**/ @SpringBootApplication //等同于 @Configuration @EnableAutoConfiguration @ComponentScan public class RestMain {public static void main( String[] args ){SpringApplication.run(RestMain.class, args); } }
3.BeanConfig.java
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration @ComponentScan(basePackages="com.maven") //这是扫描的包的主路径 @EnableAspectJAutoProxy //AOP代理自动配置 public class BeanConfig { // @Bean // public AppService service(){ // return new AppService(); // }// @Bean // public User user(){ // return new User(); // }// @Bean // public ContactBook contactBook(){ // return new ContactBook(); // }// @Bean // public AspectConcern aspectConcern(){ // return new AspectConcern(); // } }
4.WebConfig.java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter{/*** 这里的@bean()需要赋值一个名称,否则会抛出没有创建这个bean的异常* */@Bean("defaultServletHandlerMapping")public ViewResolver viewResolver(){InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("WEB-INF/views/");resolver.setSuffix(".jsp");resolver.setExposeContextBeansAsAttributes(true);return resolver;}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}}
5.LoginController.java
import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;import com.maven.demo.model.User;@Controller public class LoginController {@RequestMapping(value="/login",method=RequestMethod.GET)@ResponseBodypublic String getting(HttpServletResponse response) throws IOException{//设置成功时的响应码response.setStatus(606);//请求重定向response.sendRedirect("http://www.baidu.com");return "<div>Hello</div>";} /*** @param user 从体中获取的json数据(体中的数据与User里面的属性一一对应)* @param pathValue 从url地址中获取的路径名称* @param name 从url地址中获取的对应的参数的值(defaultValue表示默认值)* @param id* @return*/@RequestMapping(value="/login/{pathValue}",method=RequestMethod.POST)@ResponseBodypublic User posting(@RequestBody User user,@PathVariable String pathValue,@RequestParam(defaultValue="tianheng")String name,int id){ return user;} }
6.User.java
import java.sql.Date; import java.util.List;import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;/*** 用户实体类**/ @Component //@ConfigurationProperties(prefix="my",locations = "classpath:application.yml") //@ConfigurationProperties(prefix="my") //配合测试类@SpringBootTest()使用 //@Entity //@NamedQueries({ //@NamedQuery(name = "User.findByNameWithNamedQuery", //query = "select c from User c where c.name = ?1")}) public class User {//@Idprivate int id;private String name;private char sex;private Date birthDate;private int height;//@OneToMany(mappedBy="myUser")private List<ContactBook> contact;public User(){}public User(int id ,String name,char sex,Date birthDate,int height,List<ContactBook> contact){this.id = id;this.name = name;this.sex = sex;this.birthDate = birthDate;this.height = height;this.contact = contact;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public Date getBirthDate() {return birthDate;}public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}public List<ContactBook> getContact() {return contact;}public void setContact(List<ContactBook> contact) {this.contact = contact;}public void setName(String name) {this.name = name;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}}
7.pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source> <!-- Java版本,不要在build path里面修改 --><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.5</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.7.RELEASE</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.4.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>1.4.0.RELEASE</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>1.4.0.RELEASE</version><optional>true</optional> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.4.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>1.4.0.RELEASE</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>1.4.192</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
8.运行RestMain.java文件
9.
结果:
Spring----Spring Boot Rest的使用方法相关推荐
- Spring boot异常统一处理方法:@ControllerAdvice注解的使用、全局异常捕获、自定义异常捕获
Spring boot异常统一处理方法:@ControllerAdvice注解的使用.全局异常捕获.自定义异常捕获 参考文章: (1)Spring boot异常统一处理方法:@ControllerAd ...
- 转:Spring Boot 获取 HttpServletRequest 的方法
转自: Spring Boot 获取 HttpServletRequest 的方法 - 简书本文介绍 Spring Boot 2 获取 HttpServletRequest 的方法. 目录 概述 方法 ...
- Spring Boot 启动执行某个方法的三种实现方式
Spring Boot 启动执行某个方法的三种实现方式 Spring Boot 启动执行某个方法的三种实现方式 第一种方式 @PostConstruct 注解解释 具体实现 第二种方式 Applica ...
- Spring - Spring Boot Spring Cloud
Spring -> Spring Boot > Spring Cloud 这几天刚刚上班,公司用的是Spring Cloud,接触不多.我得赶快学起来. 想学习就必须得知道什么是微服务,什 ...
- Spring MVC Boot Cloud 技术教程汇总
转载自 Spring MVC & Boot & Cloud 技术教程汇总 昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技 ...
- Spring MVC Boot Cloud 技术教程汇总(长期更新)
昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ ...
- Spring+Spring Boot+Mybatis框架注解解析
Restful 风格下的Spring Boot的注解开发 电商网站经常用到的restful风格 ,只是一种开发思想,不是开发框架,现在的技术并没有完全实现restful风格. restful风格是一种 ...
- Spring Boot--Druid连接池的配置方法
原文网址:Spring Boot--Druid连接池的配置方法_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍Spring Boot的Druid连接池的配置方法. Druid 是阿里巴巴开源的 ...
- Spring/Spring Boot 常用注解总结
0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot ...
- Servlet自动注入Spring容器中的Bean解决方法
Servlet自动注入Spring容器中的Bean解决方法 参考文章: (1)Servlet自动注入Spring容器中的Bean解决方法 (2)https://www.cnblogs.com/jank ...
最新文章
- First Week :Linux系统学习
- volatile与synchronized 同步原理基础讲解
- 黑色幽默:“新知青”电影《走着瞧》首映
- linux+删除乱码的文件,linux 下删除乱码文件-乾颐堂
- 少儿编程150讲轻松学Scratch(十一)-用Scratch算法给矩形工具填充颜色
- 小程序实现无限瀑布流
- css页面布局的感想,css布局实践感想(示例代码)
- 把Sublime Text3从windwos移到ubunut上
- 网易游戏回应裁员 10%;字节跳动秘密研发手机;iOS 13 beta 5 发布 | 极客头条
- 百度牵手“造王者”汉得,ToB 战场将迎大变局?| 畅言
- 谷歌宣布开源 Live Transcribe 语音识别转文字工具
- Python 列表深浅复制详解
- Lan内部是如何完成通信的
- 推荐一个微软知识库(Microsoft Knowledge Base)订阅
- 【2023】浙江大学计算机考研信息汇总
- The Hidden Agenda User Simulation Model翻译
- 脚本自动ping检测网络情况
- 【解决】npm ERR A complete log of this run can be found in npm ERR
- 基于JSP+Mysql java教师教学质量测评系统
- 95-2.Hive史诗级调优大全_ev 两个案例重复了 没用
热门文章
- Oracle Clustered Table
- 博客园----你真的没有没落.感恩博客园。。。
- 计算机网络——OSI与TCPIP体系架构、功能及协议
- 如何让linux的history命令显示时间记录
- JS Array.reduce 实现 Array.map 和 Array.filter
- 学界 | 终结吧!机器学习的数学焦虑
- App.config的典型应用
- Nginx中worker_connections的问题
- 2018/11/11蓝桥杯Java培训
- 分布式缓存Redis Centos下单节点安装