After an introduction, I believe that the small partners have pressed the heart of springboot yearning, this article I will continue to the small partners to introduce the configuration of Springboot configuration files, has been the global configuration parameters how to use, OK below start our today's content introduction.
We know that spring boot supports automatic configuration of containers, which by default is Tomcat, and of course we can modify them as well:
1, first we exclude spring-boot-starter-web-dependent tomcat: exclude Tomcat in the Pom file starter
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> <Exclusions> <exclusion> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-tomcat</Artifactid> </exclusion> </Exclusions></Dependency>
2. Add Jetty Container
< Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-jetty</artifactid> </ Dependency >
This way our Springboot container is modified into a jetty container.
In order to facilitate our debugging, here we recommend an HTTP debugging tool: Postman
Let's talk about the Springboot global configuration file: Application.properties
In the development must have encountered such a requirement, is to modify our container access port, since Springboot loading the container by default, then the port setting is certainly controlled by the configuration file, it is quite convenient we just need to add in the configuration file:
server.port=6666
This will change our container port to 6666.
We can also set the project Access alias through the configuration file:
Server.context-path=/springboot1
This allows us to launch the project via HTTP://LOCALHOST:6666/SPRINGBOOT1 to access our project
The above is just the tip of the Springboot configuration file configuration, for example, we can also set up a database connection configuration (DB), set up the development environment configuration, deploy the environment configuration, and achieve a seamless transition between the two.
Let's take a look at the use of the controller for Springboot, and Springboot provides us with three annotations:
The last one we used is @restcontroller, let's use @controller to try it out:
@Controller // @ResponseBody Public class requesttest { /** * does not restrict request mode @return */ = "/req") public String req () { return "Success" ; }}
When we entered Http://localhost:8080/springboot1/req enter in the browser, found 404
{ "timestamp": 1515332935215, "status": 404, "error": "Not Found", "message": "No message Available", "path": "/springboot1/req"}
What is this for? This is because @controller must be used with templates, so here we open Maven Pom files and add spingboot templates:
<!--springboot template--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
Then find templates in the resources directory of our project (if no, create a new one, but be sure to note that the folder name must be consistent), and then create a success.html so that we start the project again, access just the address, is not OK.
But here to explain a point, now the enterprise-level development is a front-end separation, we do the background service only need to return the corresponding data can, of course, the use of templates there is a disadvantage, that is, performance will cause a certain loss, so here everyone simple to understand.
The above introduction has already said, @[email protected] equivalent to @restcontroller, so it is recommended that you use @restcontroller.
Here we introduce the @requestmapping (value = "/req"), this note is believed to have known his usage, of course, this annotation can not only be used in the method, the same applies to the class.
@RestController//@Controller//@ResponseBody@RequestMapping (value = "/test") Public classRequesttest {/*** Not restricted by request method *@return */@RequestMapping (Value= "/req") PublicString req () {return"Success"; } /*** Limit request mode to get *@return */@RequestMapping (Value= "/req1", method =requestmethod.get) PublicString req1 () {return"Success"; } /*** Limit the request mode to post *@return */@RequestMapping (Value= "/req2", method =requestmethod.post) PublicString req2 () {return"Success"; }}
For method Trust see here you must already know his usefulness, yes. The specified access type is not set by default and can be accessed in any way. Do not know if the small partners think if you set the method in the @requestmapping of the class, then the methods in the class default inheritance, of course, can also be set at the method, the priority of the problem, the small partner himself to try it.
Below I will show you how to access constants in a configuration file in a controller. First we add in the configuration file:
Name=hpugsage=35content=name:${name};age:${age}
We use constants in the config file, using ${}.
Here we inject parameters into the controller:
//parameters in the injected configuration file@Value ("${name}") PrivateString name; @Value ("${age}") PrivateInteger age; @Value ("${content}") PrivateString content; @RequestMapping (Value= "/req3", method =requestmethod.get) PublicString req3 () {return"Name=" +name; } @RequestMapping (Value= "/req4", method =requestmethod.get) PublicString req4 () {return"Age=" +Age ; } @RequestMapping (Value= "/req5", method =requestmethod.get) PublicString req5 () {return"Content=" +content; }
Start our project visit and try it out.
This kind of use if you feel not good enough, here to teach you a trick: we use the class mapping configuration file, using the class for parameter usage, relative to a single parameter injection to facilitate some, first create a Java class
@Component @configurationproperties (prefix= "UserInfo") Public classUserInfo {PrivateString names; PrivateInteger age; PrivateString content; PublicInteger getage () {returnAge ; } PublicString GetNames () {returnnames; } Public voidsetnames (String names) { This. Names =names; } Public voidsetage (Integer age) { This. Age =Age ; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; }}
Then set the parameters in our configuration file:
userinfo.names= Small child userinfo.age=25userinfo.content=name:${userinfo.names};age:${userinfo.age}
Wiring to enable our controller:
//Inject Object@AutowiredPrivateUserInfo UserInfo; @RequestMapping (Value= "/req6", method = Requestmethod.get, produces= "Text/plain;charset=utf-8") PublicString Req6 () {return"Name=" +Userinfo.getnames (); } @RequestMapping (Value= "/req7", method =requestmethod.get) PublicString req7 () {return"Age=" +Userinfo.getage (); } @RequestMapping (Value= "/req8", method =requestmethod.get) PublicString Req8 () {return"Content=" +userinfo.getcontent (); }
Okay, let's try restarting our project visit.
Did the little friends know about the problem? Appeared in Chinese garbled, first of all do not worry, we first look at another kind of springboot configuration file: Application.yml. This configuration file replaces ";" with newline spaces, and we'll look at how the same configuration is configured under YML:
Server: port:8888 context-path:/springboot1name:hpugsage:35content:name:${name};age:${age}userinfo: names: Small child age:25 Content:name:${userinfo.names};age:${userinfo.age}
Now let's start the project run and try it.
Back to the above garbled problem, when we use yml is not garbled, small partner is not a bit depressed, this is why? This is because the. properties file uses Unicode encoding, so when we enter Chinese, garbled characters appear. Of course, there's another reason why I can encode settings and front-end inconsistencies, which we add in the configuration file:
Spring: http: encoding: force:true charset:utf-8 enabled:trueserver: Tomcat: Uri-encoding:utf-8
To take control. Here we introduce the development tips, Springboot provides us with different configuration file solutions in various development environments:
#yml格式spring: Profiles: active:prod#.properties format Spring.profiles.active=dev
All right, here's the Springboot controller. Here's how the next Springboot controller can be accessed.
If the above content is wrong, please do not give the enlighten. Thank you
Springboot into pits-spring-boot-starter-web profile usage