First Spring-boot Project

Source: Internet
Author: User
Tags getmessage

1. configuration file  application.yml
Spring
Profiles
Active:dev
DataSource
Driver-class-name:oracle.jdbc.driver.oracledriver
Url:jdbc:oracle:thin: @localhost: 1521:xe
Username:system
password:123456
Jpa:
Hibernate:
Ddl-auto:update
Show-sql:true
1. Read the contents of the configuration file.
Server:  port:8081  context-path:/bootservice:  Name:freya
version:1.1

(1) [Email protected] ("${service.name}")

private String name;

(2). Use Environment,env.getproperty ("Key Name")

@Autowired
    Private environment env;    @RequestMapping (value = "/hello") public    String Hello () {        return "Hello Spring boot!" + env.getproperty ("service . Name ");    }
(3)
Customizing an entity class
@Component @configurationproperties (prefix = "project")  //config file prefix @propertysource (value = "classpath:config/ Config.properties ")//File location public class Myconfig {    private String version;    private String name;    Public String getversion () {        return version;    }    public void Setversion (String version) {        this.version = version;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }}
Get it in the controller
Restcontrollerpublic class Hellocontroller {    @Autowired    private myconfig myconfig;    @RequestMapping (value = "/hello") public    String Hello () {        return "Hello Spring boot!" myconfig.getversion () + "" +  myconfig.getname ();    }}
2. Form submission
Controller layer
@PostMapping ("/save")
Public Girl Save (@Valid Girl Girl, Bindingresult bindingresult) {
if (Bindingresult.haserrors ()) {
System.out.print (Bindingresult.getfielderror (). Getdefaultmessage ());
return null;
}
Girl.setage (Girl.getage ());
Girl.setcupsize (Girl.getcupsize ());
Girl.setmoney (Girl.getmoney ());
Return Girlrepository.save (Girl);
}

Entity class

@Entity  //tables in the corresponding database
public class girl{

@Id//Primary key
@GeneratedValue self-increment, starting from 1
Private Integer ID;

@Min (value = 18,message = "underage") //Minimum value of 18, does not satisfy the value of return message
Private Integer age;
Private String cupsize;

@Min (value = 0,message = "Amount must be passed")
Private Double money;
3. Custom exception (CodeEnum is enumerated type, easy to manage exception status code and return information)
public class Girlexception extends runtimeexception{
Private String Respcode;


Super (Codeenum.getmsg ());
This.respcode = Codeenum.getcode ();
}
Public girlexception (String message,string code) {
Super (message);
This.respcode = code;
}
Public String Getrespcode () {
return respcode;
}

public void Setrespcode (String respcode) {
This.respcode = Respcode;
}
}

Enum entity public enum CodeEnum {
  Error_code ("1", "System Exception"), 
Ok_code ("0", "OK"),
Pre_school ("100", "You may still be in primary school bar"),
High_school ("101", "You may still be in high school");

CodeEnum (string code, String msg) {
This.code = code;
this.msg = msg;
}
Private String code;
Private String msg;

Public String GetCode () {
return code;


Public String getmsg () {
return msg;
}
}
/**
  * catch Exception class (handle) 
*/
@ControllerAdvice
public class Exceptionhandle {

@Except Ionhandler
@ResponseBody
Public resultmap handle (Exception e) {
if (e instanceof girlexception) {
Girlexception girlexception = (girlexception) e;
Return Resultmap.error (Girlexception.getrespcode (), Girlexception.getmessage (), "");
}
Return Resultmap.error (E.getmessage ());
}
}

/**
* spring-boot ao
*/
@Aspect
@Component
public class Httpaspect {

Private static final Logger log = Loggerfactory.getlogger (Httpaspect.class); The log package that comes with spring

@Pointcut ("Execution (* com.girl.controller.girlcontroller.* (..))")
public void log () {}

@Before ("log ()")
public void Log1 () {
GET request Requests
Requestattributes RA = requestcontextholder.getrequestattributes ();
Servletrequestattributes SRA = (servletrequestattributes) RA;
HttpServletRequest request = Sra.getrequest ();
Log.info ("id={}", Request.getremoteaddr ()); The log information is output in {}

}
}

Multi-Module deployment:
https://www.imooc.com/video/16354

First Spring-boot Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.