After the introduction of the previous article, I believe that my friends can no longer help but yearn for springboot. In this article, I will continue to introduce the configuration of springboot configuration file to my friends. How to use the global configuration parameters. OK, let’s start our introduction to today’s content.
We know that Spring Boot supports automatic configuration of containers, and the default is Tomcat, of course we can also modify it:
1. First, we exclude Tomcat in spring-boot-starter-web dependency: Exclude tomcat's starter in pom file
<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 to Jetty container
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId></dependency>
In this way, our springboot container will be modified into a Jetty container.
To facilitate our debugging, here is a http debugging tool for you: Postman
Let's talk about the global configuration file of springboot: application.properties
In development, we must have encountered such a requirement, which is to modify our container access port. Since springboot loads the container by default, the port settings are of course controlled through configuration files, which is quite convenient. We only need to add it in the configuration file:
server.port=6666
In this way, our container port will be modified to 6666.
We can also set project access alias through configuration files:
server.context-path=/springboot1
In this way, we can start the project and access our project through http://localhost:6666/springboot1
The above is just the tip of the iceberg of springboot configuration file configuration. For example, we can also set up database connection configuration, set up development environment configuration, deploy environment configuration, and achieve seamless switching between the two.
Let’s learn about the use of springboot’s controller. Springboot provides us with three annotations:
In the previous article, we used @RestController. Let's try using @Controller together:
@Controller//@ResponseBodypublic class RequestTest { /** * No restrictions on request method* @return */ @RequestMapping(value = "/req") public String req(){ return "success"; }}When we enter http://localhost:8080/springboot1/req to enter in the browser, we find 404
{ "timestamp": 1515332935215, "status": 404, "error": "No message available", "path": "/springboot1/req"}Why is this? This is because @Controller must be used with the template, so we open the maven pom file and add the template of springboot:
<!-- 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 not, 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 and access the address just now, is it OK?
However, it should be noted here that the current enterprise-level development is separated from the front and back ends. When we do back-end services, we only need to return the corresponding data. Of course, there is another disadvantage to using templates, that is, performance will cause certain losses, so everyone can briefly understand it here.
As mentioned in the above introduction, @Controller+@ResponseBody is equivalent to @RestController, so it is recommended that you use @RestController here.
Let’s introduce @RequestMapping(value = "/req"). I believe everyone already knows its usage. Of course, this annotation can not only be used in methods, but also applies to classes.
@RestController//@Controller//@ResponseBody@RequestMapping(value = "/test")public class RequestTest { /** * No restrictions on request method* @return */ @RequestMapping(value = "/req") public String req(){ return "success"; } /** * Restricted request method is GET * @return */ @RequestMapping(value = "/req1", method = RequestMethod.GET) public String req1(){ return "success"; } /** * The restriction request method is POST * @return */ @RequestMapping(value = "/req2", method = RequestMethod.POST) public String req2(){ return "success"; }}I believe that when you see this method, you must already know its usefulness. It is a specified access type, and it can be accessed in any way without setting the default. I don’t know if I thought that if the method is set in the @RequestMapping class, the method in the class will be inherited by default. Of course, you can also set it separately at the method. Please try the priority issue yourself.
Below I will introduce to you how to access constants in configuration files in Controller. First we add in the configuration file:
name=hpugsage=35content=name:${name};age:${age}We use constants in configuration files, which are used by ${}.
Below we inject parameters in the Controller:
//Inject parameters in the configuration file @Value("${name}") private String name; @Value("${age}") private Integer age; @Value("${content}") private String content; @RequestMapping(value = "/req3", method = RequestMethod.GET) public String req3(){ return "name=" + name; } @RequestMapping(value = "/req4", method = RequestMethod.GET) public String req4(){ return "age=" + age; } @RequestMapping(value = "/req5", method = RequestMethod.GET) public String req5(){ return "content=" + content; }Start our project and try to visit it.
If you feel unsatisfied with this use, here is another trick: we use class mapping configuration files and use classes to use parameters. It is more convenient than injecting a single parameter. First, create a Java class
@Component@ConfigurationProperties(prefix = "userInfo")public class UserInfo { private String names; private Integer age; private String content; public Integer getAge() { return age; } public String getNames() { return names; } public void setNames(String names) { this.names = names; } public void setAge(Integer age) { this.age = age; } public String getContent() { return content; } public void setContent(String content) { this.content = content; }}Then set the parameters in our configuration file:
userInfo.names=Little Broken Child userInfo.age=25userInfo.content=name:${userInfo.names};age:${userInfo.age}Wiring to make our Controller:
//Inject object @Autowired private UserInfo userInfo; @RequestMapping(value = "/req6", method = RequestMethod.GET, produces="text/plain;charset=UTF-8") public String req6(){ return "name=" + userInfo.getNames(); } @RequestMapping(value = "/req7", method = RequestMethod.GET) public String req7(){ return "age=" + userInfo.getAge(); } @RequestMapping(value = "/req7", method = RequestMethod.GET) public String req7(){ return "age=" + userInfo.getAge(); } @RequestMapping(value = "/req8", method = RequestMethod.GET) public String req8(){ return "content=" + userInfo.getContent(); }OK, try restarting our project visit.
Friends, don’t you know if you encounter this problem? There is garbled Chinese code. First of all, don’t worry. Let’s look at another springboot configuration file: application.yml. This configuration file replaces ";" by newline spaces. Let's take a look at how the same configuration is configured under yml:
server: port: 8888 context-path: /springboot1name: hpugsage: 35content: name:${name};age:${age}userInfo: names: little kid age: 25 content: name:${userInfo.names};age:${userInfo.age}Now let's start the project and try it.
Going back to the garbled question above, is there no garbled when we use yml? Are you a little depressed? Why is this? This is because the .properties file uses the unicode encoding form, so garbled code will appear when we enter Chinese. Of course, there is another reason for garbled code, that is, the encoding settings I can be inconsistent with the front-end. We add this to the configuration file:
spring: http: encoding: force: true charset: UTF-8 enabled: trueserver: tomcat: uri-encoding: UTF-8
to control. Here are some development tips. Springboot provides us with solutions to different configuration files in different development environments:
#yml format spring: profiles: active: prod#.properties format spring.profiles.active=dev
Summarize
The above is the use of the SpringBoot pitfall notes of the SpringBoot pitfalls introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!