SpringBoot + SpringCloud climb pit tour

1, application.yamlthe configuration does not become effective problem-solving

If the configuration file to confirm no errors but does not take effect first of all it is to see whether the build directory to be compiled later, if not, please restart the project clean
but when the idea to start the project will first build, there may be no configuration file compile the past, the true father of the pit!
Further, those pit YAML file:
(1) the colon: Must be followed by a space, a lower grid indentation properties (space not only supports the support tabs Tab)
(2) can not guarantee a duplicate node.
(3) If the argument is a space-start or end of a string, use single quotes wrapped him in. If a string parameter contains special characters, also wrapped in single quotes.
If the string contains a single quote itself, you need to use 'escape; if the beginning or end of the string contains spaces need to enclose the entire string in single package

2, SpringBoot context-path profile ineffective

SpringBoot 2.0.0.RELEASE updated version

  • yml writing:
server:
    servlet:
        context-path: /example
  • properties wording:
server.servlet.context-path=/example
    

3,Unable to find main class

Background: spring-boot program, packaged into an executable jar, there are two classes with main methods are used @SpringBootApplication and annotations (within the project or another situation: You have two main methods and where classes were not used @SpringBootApplication comment)
Here Insert Picture Description

4, when the kick-executable JAR package found in a very small output maven local repository, does not refer to all JAR modules.

        <plugins>
            <plugin>
                <!--该插件主要用途:构建可执行的JAR -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration><!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.sbcm.UserApplication</mainClass>
                    <layout>ZIP</layout>
                    <outputDirectory>
                        ${package.base.url}
                    </outputDirectory>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

5, skipping the test case

At work, in many cases we do not want to pack execution of test cases, test cases may be incomplete thing, or test cases will affect the database data.

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

6, which does not recognize dao interface, suggesting no bean

Here Insert Picture Description
启动抱错:
Here Insert Picture Description
这里我是在SpringBoot中集成MyBatis,使用的旧的xml文件写sql的集成方式,我以为只有用新的注解方式写sql才会需要@Mapper注解和@MapperScan注解,其实就算用旧的xml方式也需要在入口文件加上@MapperScan注解才能扫描到dao文件,@Mapper倒是不需要。
Here Insert Picture Description

7,@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 的 作用是 :取消数据库配置。

所以在用到数据库的时候记得将他改为 @SpringBootApplication ,

否则会报错:如下

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跨模块调用问题

背景:
项目两个独立模块user和task,两个独立的服务提供模块,现在只启动了user模块,想在浏览器访问task模块的服务。

方法:
1,把task也启动,这样最简单,不过有多个其他模块也要访问的话只能都启动,明显不太方便。

2,把task加到user的依赖里面去,就是在user的pom里面添加task模块,还需要在user的扫描的注解@ComponentScan里面扩大范围,让他能扫描到task的注解,不然task的服务也找不到,不过这样会导致强耦合,因为user和task是两个独立的服务模块

3,使用Spring cloud,把user和task注册到服务中心,互相通过serverName调用。(推荐)

9,spring boot与cloud构建微服务,返回数据从json格式 变成了 xml格式

1、问题:

 本身spingboot项目是用@RestController注解,返回结果也是json格式,但是结合springcloud的eureka构建微服务之后,无论是消费者还是提供者,均返回的xml格式

2、分析

今天正好遇到了这个问题,查阅了很多东西大致弄明白了。引入了jackson-dataformat-xml这个依赖,它是提供了jackson将实体类转化为xml相关的作用。而本身jackson是可以将实体类转化为json的,所以这样Jackson是可以将实体类转化为两种类型的数据,而具体要转化为哪一种数据,是要看http请求里面的accept头信息的,我的浏览器chrome的accept是 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 ,然后服务器会根据accept来决定是返回xml还是json,由于浏览器accept只有最后的/是匹配 application/json的,****而application/xml在/前面,优先级比json高,所以用浏览器直接调用是会优先返回xml格式的。****

3、解决方案有两种:

1.自己调用接口的时候修改accept信息,改为application/json (postman之类的工具)
提供者与消费者的方法上或者所属类上添加 produces=“application/json”,
Here Insert Picture Description
2.添加依赖

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

Then you can use the suffix to call the relevant interface to obtain data corresponding to the format. For example, I have a url localhost / get / user returns a user-dependent data added after the above, if you want to get xml format, use localhost / get / user.xml to call interface; if you want to get json format will use localhost /get/user.json to call interface its principle is based on the suffix initiative to amend the server accept information

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.

Here Insert Picture Description
If you see this Eureka Server home page the following tips, then Eureka has entered the protection mode:
This mode usually occurs when the service returns an error. If that is true service has been Down, but has been present in the registry interface service, and the display state to UP.
Reference: https: //blog.csdn.net/cvntopuyef/article/details/78477681

Guess you like

Origin www.cnblogs.com/both-eyes/p/11106255.html