Skip to content

Develop Problems

Fanyuepan edited this page Mar 16, 2019 · 39 revisions

1.问:Eureka客户端(fyl-user-service)无法调用Controller方法,莫名其妙跳到 /login

答: 由于之前在父pom.xml中引入了spring-boot-starter-security,导致security在没有配置任何拦截的情况下执行默认拦截器。去掉引用,然后 maven-reimport即可 总结:starter不能乱用。会自动加载某些功能。

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

2.问:Eureka Client为什么注册出现错误:401?

答: SpringCloud版本问题,注意区别。新版的需要增加相应的配置类

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().ignoringAntMatchers("/eureka/**");
        //开启认证
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

3.问:Config Client访问CONFIG-CLIENT-SERVER/refresh为什么会返回404?

答: 先确定是否引入spring-boot-starter-actuator

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

SpringBoot2.0+方式和SpringBoot1.0+不一样,由于项目使用的是2.0,所以配置方式如下:Config Server,Config Clientbootstrap.yml都加上

management:
  endpoints:
    web:
      exposure:
        include: '*'

4.问: spring-cloud-starter-cbus-amqp引入项目之后,启动报错:

Failed to introspect Class [org.springframework.cloud.stream.config.BindingServiceConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]

答: 解决办法:版本不兼容导致。版本兼容情况参考:

{
	"git": {
		"commit": {
			"time": "2019-03-01T08:09:44Z",
			"id": "9ac9c9f"
		},
		"branch": "9ac9c9f14a24dccd2055df397b0400a1c63ac351"
	},
	"build": {
		"version": "0.0.1-SNAPSHOT",
		"artifact": "start-site",
		"name": "start.spring.io website",
		"versions": {
			"initializr": "0.8.0.BUILD-SNAPSHOT",
			"spring-boot": "2.1.3.RELEASE"
		},
		"group": "io.spring.start",
		"time": "2019-03-01T10:40:38.173Z"
	},
	"bom-ranges": {
		"azure": {
			"0.2.4": "Spring Boot >=1.5.4.RELEASE and <2.0.0.RELEASE",
			"2.0.10": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
			"2.1.2": "Spring Boot >=2.1.0.RELEASE"
		},
		"codecentric-spring-boot-admin": {
			"1.5.7": "Spring Boot >=1.5.9.RELEASE and <2.0.0.M1",
			"2.0.5": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
			"2.1.3": "Spring Boot >=2.1.0.M1"
		},
		"spring-cloud": {
			"Edgware.SR5": "Spring Boot >=1.5.0.RELEASE and <=1.5.19.RELEASE",
			"Edgware.BUILD-SNAPSHOT": "Spring Boot >=1.5.999.BUILD-SNAPSHOT and <2.0.0.M1",
			"Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
			"Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
			"Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
			"Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
			"Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
			"Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
			"Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
			"Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
			"Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
			"Finchley.SR3": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
			"Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
			"Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
			"Greenwich.RELEASE": "Spring Boot >=2.1.0.RELEASE and <2.1.4.BUILD-SNAPSHOT",
			"Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.4.BUILD-SNAPSHOT"
		},
		"spring-cloud-services": {
			"1.6.6.RELEASE": "Spring Boot >=1.5.0.RELEASE and <=1.5.999.BUILD-SNAPSHOT",
			"2.0.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <=2.0.999.BUILD-SNAPSHOT",
			"2.1.0.RELEASE": "Spring Boot >=2.1.0.RELEASE"
		},
		"spring-statemachine": {
			"2.0.0.M4": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
			"2.0.0.M5": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
			"2.0.1.RELEASE": "Spring Boot >=2.0.0.RELEASE"
		},
		"vaadin": {
			"8.7.0": "Spring Boot >=1.5.0.RELEASE and <2.0.0.M1",
			"10.0.11": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
			"12.0.7": "Spring Boot >=2.1.0.M1"
		}
	},
	"dependency-ranges": {
		"cxf-jaxrs": {
			"3.2.5": "Spring Boot >=1.5.0.RELEASE and <2.1.0.M1"
		},
		"ratpack": {
			"1.1.1": "Spring Boot >=1.5.0.RELEASE and <2.0.0.M1"
		},
		"okta": {
			"1.1.0": "Spring Boot >=2.1.2.RELEASE"
		},
		"mybatis": {
			"1.3.3": "Spring Boot >=1.5.0.RELEASE and <2.0.0.RELEASE",
			"2.0.0": "Spring Boot >=2.0.0.RELEASE"
		},
		"open-service-broker": {
			"2.1.0.RELEASE": "Spring Boot >=2.0.0.RELEASE"
		},
		"camel": {
			"2.21.4": "Spring Boot >=1.5.0.RELEASE and <2.0.0.M1",
			"2.22.2": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
			"2.23.1": "Spring Boot >=2.1.0.M1"
		}
	}
}

5. 问:Config Server增加Http Basic认证,访问/actuator/bus-refresh 出现403问题?

答: 首先,配置文件添加:

management:
  endpoints:
    web:
      exposure:
        include: '*'

另外,开启http basic认证方式为httpbasic,这里需要注意的是,如果使用restClient或者curl的方式,要增加 http.csrf().disable();否则还是无法刷新配置。

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //禁用csrf
        http.csrf().disable();
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

6.问: Springboot Thymeleaf使用layout死活加载不上headerfooter,怎么回事?

<body>
<div th:replace="fragments/header::header"></div>
<div layout:fragment="content" id="content" >123456</div>
<div th:replace="fragments/footer::footer"></div>
</body>

其他页面引入

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml" layout:decorator="layout">
<head th:include="layout :: htmlhead" th:with="title='fly-layui'"></head>
<body>
<div layout:fragment="content"></div>
</body>
</html>

答: SpringBoot2.1.3版本的spring-boot-starter-thymeleaf不包含thymeleaf-layout-dialect包。引入问题解决:

 <dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.3.0</version>
 </dependency>

7.**问:**以下异常如何解决?

org.apache.ibatis.binding.BindingException: Parameter 'loginName' not found. Available parameters are [arg1, arg0, param1, param2]

答: sql参数改为arg0 arg1或者,增加@Param("name")

@Select("select id from fly_account where login_name=#{loginName} and login_pwd=#{loginPwd}")
Long loginByAccount(@Param("loginName") String loginName,@Param("loginPwd") String loginPwd);

8.问:Spring Boot使用redirect:page跳转如何隐式传参?

答: 可以通过RedirectAttributes解决。示例代码:

 public String login(RedirectAttributes redirect) {
            redirect.addFlashAttribute("errMsg",result.getMsg());
            return "redirect:login";
    }

接收参数代码:

 java.util.Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
        if(map != null) {
            Object paramFromPrePage = map.get("param");
        }

9.问: 使用mybatis-spring-boot-starter JDBC 报以下异常的处理方式?

You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

答: 连接字符串增加serverTimezone :jdbc:mysql://localhost:3306/fly?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8

10.问: 为什么 GATEWAY-SERVER/routes 为 404 ?

答: 首先management.security.enabled配置方式已经过期,另外需要配置

management:
  endpoints:
    web:
      exposure:
        include: '*'

然后访问的时候要带上acturatorhttp://localhost:9001/acturator/routes

{
	"/account/**": "fly-account-service",
	"/fly-account-service/**": "fly-account-service",
	"/eureka-server/**": "eureka-server",
	"/fly-config-server/**": "fly-config-server"
}

11.:如何注入静态变量?

答:

@Component
public class ContextUtils {

    private static RedisTemplate<String,String> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String,String> redisTemplate){
        ContextUtils.redisTemplate = redisTemplate;
    }
}