1,application.yamlThe configuration does not take effect

If the configuration file has no error but does not take effect, the first thing is to go to the compile directory to check whether it has been compiled. If not, please first clean the project before restart, but idea will start the project first build, and may not be compiled. In addition, the pits in yamL files: (1) colon: must be followed by a space, and the lower attributes are indent by one space (only Spaces are supported but TAB is not supported) (2) Ensure that there are no duplicate level 1 nodes. (3) If the argument is a string that begins or ends with a space, enclose it in single quotes. If a string argument contains special characters, it is also enclosed in single quotes. If the string itself contains single quotes, you need to escape with ‘ ‘. If the beginning or end of the string contains Spaces, you need to wrap the entire string in single quotes

2. Context-path does not work in the SpringBoot configuration file

SpringBoot 2.0.0.RELEASE updated later

  • Yml writing:
server:
    servlet:
        context-path: /example
Copy the code
  • The properties of writing:
server.servlet.context-path=/example
    
Copy the code

3, Unable to find main class

Background: The Spring-Boot project, packaged as an executable JAR, has two classes with main methods and both use the @SpringBootApplication annotation (or another case: You have two main methods and neither class uses the @SpringBootApplication annotation.)

4. When the package is completed, it is found in the local Maven repository that the output executable JAR is very small and does not reference all the jars of the module.

<plugins> <plugin> <! -- Main uses of this plug-in: GroupId >org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration><! UserApplication</mainClass> <layout>ZIP</layout> <outputDirectory>${package.base.url}
                    </outputDirectory>
                    <executable>true</executable> </configuration> <executions> <execution> <goals> <! Executions < executions> < executions> < executions> < executions> < executions> < executions> < executions>Copy the code

5. Skip the test cases

At work, there are many situations where we don’t want to execute test cases because the test cases are incomplete or the test cases affect the database data.

            <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
Copy the code

6, can not identify the dao interface, indicating that the bean can not be found

7, @ SpringBootApplication (exclude = DataSourceAutoConfiguration. Class) is used to cancel the database configuration.

So remember to change it to @SpringBootApplication when you’re using your database,

Otherwise, an error will be reported

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘xxController’: Unsatisfied dependency expressed through field ‘xxMapper’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘xxMapper’ defined in file [D:\workspacesidea\pear\target\classes\com\wqq\mapper\xxMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

8. Spring Boot cross-module invocation problem

Background: The project has two independent modules user and Task, two independent service providing modules. Now only the user module is started, and the user module wants to access the service of the Task module in the browser.

Start task (); / / Start task (); / / Start task ();

To add a task to user’s dependency, add a task module to user’s POM. To add a task module to user’s POM, also need to expand the scope of user’s scanned annotation @ComponentScan, so that he can scan the annotation of the task. Otherwise, the task service will not be found, but this will lead to strong coupling. Because User and Task are two separate service modules

3. Use Spring Cloud to register users and tasks in the service center and call each other through serverName. (recommended)

9. Spring Boot builds microservices with Cloud, and the returned data is changed from JSON format to XML format

1. Questions:

The spingBoot project itself is annotated with @RestController and returns results in JSON format, but after building microservices with SpringCloud's Eureka, both consumers and providers return XML formatCopy the code

2, analysis,

I happened to meet this problem today, and I have roughly figured it out after consulting a lot of things. The jackson-dataformat-xml dependency was introduced to provide Jackson’s ability to convert entity classes to XML. Jackson can convert the entity class into JSON. Therefore, Jackson can convert the entity class into two types of data. The specific type of data to be converted depends on the ACCEPT header information in the HTTP request. Accept my browser chrome is the accept: text/HTML and application/XHTML + XML, application/XML. Q = 0.9, image/webp image/apng, * / *; Q =0.8, then the server will decide whether to return XML or JSON according to accept. Since only the last */* of the browser accept matches application/json, application/ XML comes before/and has a higher priority than JSON. Therefore, direct browser calls will return XML format preferentially.

3. There are two solutions:

Produces = “application/json” on application/json (postman) provider/consumer methods or on his/her classes

<dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-xml-provider</artifactId>
    </dependency>
Copy the code

You can then use the suffix to call the relevant interface to retrieve the data in the corresponding format. For example, if I have a URL localhost/get/user that returns user data, after adding the above dependencies, if I want to get XML, I call the interface with localhost/get/user.xml. If you want to get the JSON format, use localhost/get/user.json to call the interface. The principle is that the server actively changes the accept message based on the suffix

9, EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

Blog.csdn.net/cvntopuyef/…