application.yaml
中配置没有生效问题解决若是配置文件确认没有错误可是没有生效首先是要到编译目录去查看是否被编译过去了,若是没有,请先将项目clean在重启 可是idea启动项目时也会先build,又有可能配置文件没有被编译过去,真实坑爹! 另外,yaml文件中的那些坑: (1)冒号:后面必须有空格,下级属性缩进一格(只支持空格不支持制表符tab) (2)保证不能有重复的一级节点。 (3)若是参数是以空格开始或结束的字符串,应使用单引号把他包进来。若是一个字符串参数包含特殊字符,也要用单引号包起来。 若是字符串中自己包含单引号,则须要用‘’进行转义;若是字符串开头或结尾包含空格,则须要用单引号将整个字符串包裹html
SpringBoot 2.0.0.RELEASE版本后更新java
server:
servlet:
context-path: /example
复制代码
server.servlet.context-path=/example
复制代码
背景:spring-boot项目,打包成可执行jar,项目内有两个带有main方法的类而且都使用了@SpringBootApplication注解(或者另外一种情形:你有两个main方法而且所在类都没有使用@SpringBootApplication注解) web
<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>
复制代码
在工做中,不少状况下咱们打包是不想执行测试用例的,多是测试用例不完事,或是测试用例会影响数据库数据.spring
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
复制代码
因此在用到数据库的时候记得将他改成 @SpringBootApplication ,sql
不然会报错:以下chrome
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数据库
背景: 项目两个独立模块user和task,两个独立的服务提供模块,如今只启动了user模块,想在浏览器访问task模块的服务。express
方法: 1,把task也启动,这样最简单,不过有多个其余模块也要访问的话只能都启动,明显不太方便。apache
2,把task加到user的依赖里面去,就是在user的pom里面添加task模块,还须要在user的扫描的注解@ComponentScan里面扩大范围,让他能扫描到task的注解,否则task的服务也找不到,不过这样会致使强耦合,由于user和task是两个独立的服务模块json
3,使用Spring cloud,把user和task注册到服务中心,互相经过serverName调用。(推荐)
一、问题:
自己spingboot项目是用@RestController注解,返回结果也是json格式,可是结合springcloud的eureka构建微服务以后,不管是消费者仍是提供者,均返回的xml格式
复制代码
二、分析
今天正好遇到了这个问题,查阅了不少东西大体弄明白了。引入了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格式的。
三、解决方案有两种:
1.本身调用接口的时候修改accept信息,改成application/json (postman之类的工具) 提供者与消费者的方法上或者所属类上添加 produces=“application/json”,
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
</dependency>
复制代码
而后就可使用后缀来调用相关的接口获取对应格式的数据了。好比我有个url localhost/get/user 返回一个用户数据添加了上面的依赖后,若是想获取xml格式的,就使用localhost/get/user.xml来调用接口;若是想获取json格式就要用localhost/get/user.json来调用接口它的原理是服务器根据后缀主动修改了accept信息