博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring----Spring Boot Rest的使用方法
阅读量:6968 次
发布时间:2019-06-27

本文共 7978 字,大约阅读时间需要 26 分钟。

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 @ComponentScanpublic 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@EnableWebMvcpublic 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;    }    @Override    public 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;@Controllerpublic class LoginController {    @RequestMapping(value="/login",method=RequestMethod.GET)    @ResponseBody    public String getting(HttpServletResponse response) throws IOException{        //设置成功时的响应码        response.setStatus(606);        //请求重定向        response.sendRedirect("http://www.baidu.com");        return "
Hello
"; } /** * @param user 从体中获取的json数据(体中的数据与User里面的属性一一对应) * @param pathValue 从url地址中获取的路径名称 * @param name 从url地址中获取的对应的参数的值(defaultValue表示默认值) * @param id * @return */ @RequestMapping(value="/login/{pathValue}",method=RequestMethod.POST) @ResponseBody public 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 {    //@Id    private int id;    private String name;    private char sex;    private Date birthDate;    private int height;    //@OneToMany(mappedBy="myUser")    private List
contact; public User(){} public User(int id ,String name,char sex,Date birthDate,int height,List
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
getContact() { return contact; } public void setContact(List
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

4.0.0
com.maven
demo
0.0.1-SNAPSHOT
jar
demo
http://maven.apache.org
UTF-8
1.8
1.8
junit
junit
4.5
test
org.springframework
spring-context
4.3.7.RELEASE
org.aspectj
aspectjweaver
1.8.9
org.springframework.boot
spring-boot-starter-web
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-tomcat
1.4.0.RELEASE
org.springframework.boot
spring-boot-configuration-processor
1.4.0.RELEASE
true
org.springframework.boot
spring-boot-starter-test
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-data-jpa
1.4.0.RELEASE
com.h2database
h2
1.4.192
org.springframework.boot
spring-boot-maven-plugin

8.运行RestMain.java文件

9.

结果:

 

你可能感兴趣的文章
小蚂蚁学习Redis笔记(12)——Redis重要特性——发布及订阅消息
查看>>
【ElasticSearch问题1】建立索引的时候报mapper_parsing_exception
查看>>
Struts2 核心基础总结
查看>>
Docker with CentOS
查看>>
C++基本内置类型
查看>>
js单双引号嵌套传值问题
查看>>
apache一键安装脚本
查看>>
Csharp数据格式初体验
查看>>
Unity 消息发送机制 解析
查看>>
实验-----实现基于文件验证的vsftpd虚拟用户
查看>>
VMware开机进入了命令行界面更改
查看>>
一篇文章为你深度解析 HTTPS 协议
查看>>
路由交换基础(二)——三层交换技术及动态路由
查看>>
前端复制问题
查看>>
Apache与PHP的结合、使主机访问linux80端口的方法、如果PHP不能解析,用下面的方法解决...
查看>>
堆排序
查看>>
个人开源项目 微阅
查看>>
linux 学习内容
查看>>
PYTHNON基础
查看>>
javascript正则教程
查看>>