spring-boot 项目 使用总结 世界快播

来源:博客园 时间:2023-06-18 06:07:34

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";}

关键词:

推荐内容

Copyright 2000-2021 by www.jiaoyu.cqyy.net all rights reserved

备案号:粤ICP备2022077823号-13

邮箱 : 317 493 128@qq.com