SpringBoot-spring-boot-starter-web configuration file,

Source: Internet
Author: User

SpringBoot-spring-boot-starter-web configuration file,

As described in the previous article, I believe that my friends can't help but feel eager for springboot. In this article, I will continue to introduce the configuration of the springboot configuration file and how to use the global configuration parameters, now let's start our introduction today.

We know that Spring Boot supports automatic container configuration. The default value is Tomcat. Of course, we can also modify it:

1. First, we excludeTomcat in spring-boot-starter-web dependency:Exclude tomcat starter from the 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 Jetty container

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-jetty</artifactId></dependency>

In this way, the springboot container is changed to the Jetty container.

To facilitate debugging, we recommend an http debugging tool Postman.

Next, let's talk about the global configuration file of springboot: application. properties.

  During development, we must have met this requirement: Modify the access port of our container. Since springboot loads the container by default, the port settings are controlled by the configuration file, we only need to add the following in the configuration file:

server.port=6666

In this way, our container port is changed to 6666.

We can also set the project access alias through the configuration file:

server.context-path=/springboot1

In this way, we can start the project through 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 database connection configuration, Development Environment configuration, and deployment environment configuration to achieve seamless switching between the two.
Next, let's take a look at the use of springboot's controller. springboot provides three Annotations:

In the previous article, we used @ RestController. Let's try @ Controller together:

@ Controller // @ ResponseBodypublic class RequestTest {/*** no restrictions on request methods * @ return */@ RequestMapping (value = "/req") public String req () {return "success ";}}

When we enter http: // localhost: 8080/springboot1/req in the browser, we find 404

{    "timestamp": 1515332935215,    "status": 404,    "error": "Not Found",    "message": "No message available",    "path": "/springboot1/req"}

Why? This is because @ Controller must be used with the template, so here we open the maven pom file and add the spingboot template:

<! -- Springboot template --> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-thymeleaf </artifactId> </dependency>

Then we can start the project again and access the address just now. Is it OK.

However, it should be noted that enterprise-level development is now based on frontend and backend separation. As a backend service, we only need to return the corresponding data. Of course, there is another drawback when using templates, that is, the performance will cause a certain amount of loss, so here you can simply understand.

As mentioned in the above introduction, @ Controller + @ ResponseBody is equivalent to @ RestController, so we recommend that you use @ RestController here.

Next, let's introduce @ RequestMapping (value = "/req"). I believe you already know how to use this annotation. Of course, this annotation can not only be used in methods, the same applies to classes.

@ RestController // @ Controller // @ ResponseBody @ RequestMapping (value = "/test ") public class RequestTest {/*** does not limit the Request Method * @ return */@ RequestMapping (value = "/req") public String req () {return "success ";} /*** restrict the REQUEST method to get * @ return */@ RequestMapping (value = "/req1", method = RequestMethod. GET) public String req1 () {return "success";}/*** the request method is POST * @ return */@ RequestMapping (value = "/req2 ", method = RequestMethod. POST) public String req2 () {return "success ";}}

For method, we believe that you already know its usefulness here. Yes, the access type is specified, and no default access method is set. I don't know if my friend thinks that if method is set in @ RequestMapping of the class, the method in the class will inherit by default. Of course, you can also set it separately in the method. If you have a problem with the priority, try it yourself.

Next I will introduce how to access constants in the configuration file in the Controller. First, add the following in the configuration file:

name=hpugsage=35content=name:${name};age:${age}

We use constants in the configuration file through $.

Below we inject parameters in the Controller:

// Inject the parameter @ Value ("$ {name}") private String name; @ Value ("$ {age}") private Integer age in the configuration file; @ 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 again.

If you don't feel comfortable using this method, let's teach you another trick: we use the class ing configuration file and the class for parameter usage, which is easier than single parameter injection, 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 = 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; charsets = UTF-8") public String req6 () {return "name =" + userInfo. getNames () ;}@ 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 ();}

Restart our project and try again.

Do you know this problem? If Chinese characters are garbled, Do not worry first. Let's take a look at another springboot configuration file: application. yml. This configuration file replaces ";" with a line break. Let's take a look at how the same configuration is configured under yml:

Server: port: 8888 context-path:/springboot1name: hpugsage: 35 content: name :$ {name}; age :$ {age} userInfo: names: child age: 25 content: name: $ {userInfo. names}; age: $ {userInfo. age}

Now let's start the project and run it.

Back to the above garbled problem, when we use yml, isn't there any garbled code, isn't it a bit depressing, why? This is because the. properties file uses unicode encoding, so garbled characters will appear when we enter Chinese characters. Of course, there is another reason for Garbled text, that is, the encoding settings I can use are inconsistent with those of the front-end. In this case, we add the following in the configuration file:

spring:    http:        encoding:          force: true          charset: UTF-8        enabled: trueserver:  tomcat:    uri-encoding: UTF-8

. Here are some tips for development. springboot provides us with solutions for different configuration files in different development environments:

# Yml format spring: profiles: active: prod #. properties format spring. profiles. active = dev

Now let's take a look at the springboot Controller content here first. Next, how does springboot Controller Access springboot Controller with parameters.

If any of the above content is incorrect, please kindly advise. Thank you.

 

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.