栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot笔记——boot外层及接口测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot笔记——boot外层及接口测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nHECELxt-1639666357156)(D:2021mdspringbootimgspringboot简介.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L4akltS9-1639666357157)(imgspringboot和springmvc的区别.png)]

1 SpringBoot启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootHelloApplication.class, args);
	}
}

类中的@SpringBootApplication注解是springboot的核心注解,主要作用是开启spring自动配置。使用这个注解相当于加上了下面三个注解:

@Configuration 允许将其他@bean注解标识的类加入到spring容器中,相当于spring配置文件中的beans标签

@EnableAutoConfiguration 启动自动配置

@ComponentScan 会自动扫描当前包和子包下的标@Component,@Service,@Repository,@Controller的类。相当于以前spring配置文件中的context:component-scan

1.启动类在最外层的

BLUF{X9`Z]$K5.png)

2.启动类没有在最外层的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kOp8AUUp-1639666357158)(D:2021mdspringbootimgimage-20211215160828321.png)]

3.postman使用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-inM2jo51-1639666357160)(D:2021mdspringbootimgimage-20211215164357756.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p6Uw92cc-1639666357161)(C:UserswylAppDataRoamingTyporatypora-user-imagesimage-20211215164453041.png)]

1.[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ajoO6y4L-1639666357162)(D:2021mdspringbootimgimage-20211215164619275.png)]:接口请求历史记录

2.[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zzpGmDb6-1639666357163)(D:2021mdspringbootimgimage-20211215164612193.png)]:接口集合

3.[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4WNd7MUz-1639666357164)(D:2021mdspringbootimgimage-20211215164545938.png)]:选着请求方式的地方

4.具体其的请求路径

5.params:普通数据参数

6.Headers:头部参数

7.form-data:表单提交

8.raw:json参数

9.binary:二进制参数

4.测试http接口的get请求 restful风格
     * ```java
              * @re
        @RequestMapping(value = "/v1/{cid}/{uid}", method = RequestMethod.GET)
                         public Object findUser(@PathVariable("cid") String cid,
                                                @PathVariable("uid") String uid) {
                             map.put("cid", cid);
                             map.put("uid", uid);
                             return map;
                         }
                     ```

2.2 分页列表
@GetMapping("/v1/page_user")
public Object pageUser(@RequestParam(name="page",defaultValue = "1") int curPage,int limit){...}
保存用户信息
aram user

@return

​       @RequestMapping("/v1/save_user")
​       public Object saveUser(User user){...}
            @GetMapping("/v1/get_header")
            public Object testHeader(@RequestHeader("access_token") String accessToken,String id){...}
       @GetMapping("/v1/test_request")
       public Object testRequest(HttpServletRequest request){...}

3.post/put/delete 请求的测试

    3.1
           @PostMapping("/v2/login")
           public Object login(String name,String pass){
               map.clear();
               map.put("name",name);
               map.put("pass",pass);
               return map;
           }
3.2
       @PutMapping("/v2/put")
       public Object login(String id){
           map.clear();
           map.put("id",id);
           return map;
       }
3.3       @DeleteMapping("/v2/del")       public Object delete(String id){           map.clear();           map.put("id",id);           return map;       }
  1. jackson相关注解 (作用于实体类属性)

    指定字段不返回
    @JsonIgnore
    格式化日期
    @JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,locale = “zh”,timezone = “GMT+8”)
    空字段不返回
    @JsonInclude(JsonInclude.Include.NON_NULL)
    指定别名
    @JsonProperty(“xxx”)

5.静态文件的访问
有3个路径 默认在 resources下面 :  resources / static / public 


5.1 static 下面的静态文件访问方式位于static下的静态文件可以直接访问static/img/xx.pngstatic/js/xx.js访问方式: localhost:8080/img/xx.png         localhost:8080/js/xx.js5.2如果提供其他静态目录resources目录中除了static这个静态文件外,还有 resources和 public这2个目录, 三者优先级resources   >   static  >   public5.3templates中的文件不能直接访问,需要配置***引入依赖            org.springframework.boot            spring-boot-starter-thymeleaf***然后通过controller做跳转5.4 对于自定义静态路径的访问在resources下面创建test 静态文件夹, 里面放入静态文件 ,此时是不能直接访问到的,需要在配置文件配置这句话代表 ,配置静态路径的访问支持 ,默认可以不写,默认以支持. 但是如果加入了新的自定义静态路径就需要明确写出了spring.resources.static-locations = classpath:/meta-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public///加入test静态路径 ,改为spring.resources.static-locations = classpath:/meta-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/

6.文件上传
可以指定上传到当前工程 ,也可以指定上传到其他 目录

最后将工程打jar包 ,需要引入pom              org.springframework.boot        spring-boot-maven-plugin    如果打包过程报错: Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.567 s <<< FAILURE! - in ....删除当前工程的测试类继续执行打包

1.static、public、resources 是能够直接访问的文件夹

2.templates 他需要引入templates 模板依赖,然后通过后台调整页面的方式,进行访问页面

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/673326.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号