最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

spring-boot 项目 使用总结

来源:博客园

spring-boot 项目 使用总结


(资料图片仅供参考)

1.创建一个maven 项目spring-boot-parent(parent),pom 设置项目类型为pom2.创建一个模块spring initializr (java,maven,sdk17,jar,spring-web) boot-lesson13.在Application(main)方法同级包里创建一个java类:user.controller.UserController.java )@RestControllerpublic class UserController {@GetMapping("/index")public String index(){return "hello world!";}}

4.在模块里pom.xml文件配置发布启动的主类mainClassorg.springframework.bootspring-boot-maven-plugincom.denny.bootlesson1.BootLesson1Application

5.运行测试http://localhost:8080/index若端口被占用了在resource.application.properties文件添加端口号:server.port=8088

6.用maven.lifecycle.package 工具执行发布,发布完成,在target/test-classes/boot-lesson1-0.0.1-SNAPSHOT.jar 生成了发布jar文件7.复制boot-lesson1-0.0.1-SNAPSHOT.jar文件到需要发布的服务器(需先安装jdk)运行:java -jar boot-lesson1-0.0.1-SNAPSHOT.jar

8.配置对象,map可以和对象转换,将配置信息转化为javabear 实体类(application.yml-->config.java)application.yml:server:port: 8888servlet:context-path: /test

自定义的配置key value

name1: "zhangsan \n zhansan"name2: "lisi"

配置对象,map可以和对象转换,将配置信息转化为javabear

friends:lastName: zhangsanage: 20pets:- cat- dog- pigconditional:k1: v1k2: v2

Friends.java:package com.denny.bootlesson1.user.config;

import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;

import java.util.List;import java.util.Map;import java.util.Objects;

@Component@ConfigurationProperties(prefix = "friends")public class Friends {private String lastName;

public String getLastName() {    return lastName;}public void setLastName(String lastName) {    this.lastName = lastName;}public int getAge() {    return age;}public void setAge(int age) {    this.age = age;}private int age;public List getPets() {    return pets;}public void setPets(List pets) {    this.pets = pets;}@Overridepublic String toString() {    return "Friends{" +            "lastName="" + lastName + "\"" +            ", age=" + age +            ", pets=" + pets +            "}";}private List pets;public Map getConditional() {    return conditional;}public void setConditional(Map conditional) {    this.conditional = conditional;}private Map conditional;

}

UserController.java@RestControllerpublic class UserController {

@Value("${name1:default}")private String name1;@Value("${name2:default}")private String name2;@GetMapping("/names")public  String names(){    System.out.println(name1);    System.out.println(name2);    return "ok";}@Autowiredprivate Friends friends;@GetMapping("/map")public  String map(){    System.out.println(friends);    return "friends";}

关键词: