一、IDEA gradle 创建springboot项目后 cannot resolve symbol ‘SpringBootApplication’

用IDEA创建springboot项目,构建方式为gradle,创建完成后不能识别SpringBootApplication 注解

spring boot gradle项目搭建 gradle创建springboot项目_gradle


很多解决方法,比如 gradle clean,或者 file–invalidata caches/restart 都没有用

原来是因为gradle构建dependencies时,有的jar是只在运行时有效的

spring boot gradle项目搭建 gradle创建springboot项目_gradle_02


它是developmentOnly的,在编译时找不到,所以报错。

这时必须手动将这些jar包放到项目的dependencies中用于编译

具体步骤为

  • 在项目根目录下创建文件夹,比如libs
  • 然后在build.gradle文件中新建一个task,用来拷贝jar包
task copyJars(type:Copy){
from configurations.developmentOnly
into 'libs'
}
  • 然后在命令窗口中执行这个命令 gradle copyJars 因为我的已经执行过了,所以直接成功了,正常执行后会有一个过程,中间出现错误会报出来。没有错误看到BUILD SUCCESSFUL就表示完成了。这时拷贝的所有jar包就会出现在libs文件夹里
  • 然后 file–project structure–modules–dependencies,点击下面的+号添加目录,选择你刚才创建的libs文件夹,把所有jar包都加进去


    这里一定选择compile,当然默认就是,然后apply–ok,问题就解决了

二、用主类的main方法启动后,访问tomcat一直报404

解决方法,不要用main方法启动项目,要用gradle bootRun命令启动,至于为什么还不是很清楚

三、springboot 自带的网络工具 RestTemplate大坑

(这个东西真是坑死我了!!)

  1. 首先,在类中想要用@Autowired注入它的对象时会报错,
Field client in com.eqchu.project.httpRequest.Http required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
  1. 这时必须手动创建RestTemplate类的bean
@Configuration
public class MainConfig {
	 @Bean
	 public RestTemplate restTemplate() {
	     RestTemplate et = new RestTemplate();
	     return et;
	 }
}
  1. RestTemplate发送网络请求的默认编码方式是ISO_8859_1,如果贸然使用,会造成未知的乱码错误,所以使用之前最好改编码集。改编码集的方式为在上面的代码中加入一行
@Configuration
public class MainConfig {
	@Bean
	public RestTemplate restTemplate() {
		  RestTemplate et = new RestTemplate();
		  et.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
		  return et;
	}
}
  1. 然后,就是最坑的地方,RestTemplate自带的编码方式和java的URLEncode方法是不一样的,所以使用其任何的请求方法时,传入的url最好不要是String,而应该是URI对象,否则会引起参数错误。
@Test
public void testUrlEncode() {
    String url = "http://localhost:39531/access?accessKey=ASHJRK3LJFD%2BR32SADFLK%2BFASDJ%3D&name=yihuihui";
    RestTemplate restTemplate = new RestTemplate();
   
    //错误
    String ans = restTemplate.getForObject(url, String.class);
	//正确
	String ans = restTemplate.getForObject(URI.create(url), String.class);
    System.out.println(ans);
}
  1. 原因在这里,就不抄别人的了。
    因为这个原因,其传递的参数字符串中最好也不要有+号,否则会引起数据错误。

四、controller 返回字符串时会报类型错误 JSONObject can not be cast to String

controller返回List或者Map时没有问题,但是返回String就会报错,解决方法时新建一个config莱配置返回体

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.*;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        //针对字段的处理
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,// List字段如果为null,输出为[],而非null
                SerializerFeature.WriteMapNullValue,//加上后,字段为null的也会输出
                SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为”“,而非null
                SerializerFeature.WriteNullBooleanAsFalse,//Boolean字段如果为null,输出为false,而非null
                SerializerFeature.PrettyFormat  //结果是否格式化,默认为false
        );
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(fastJsonConfig);
        converters.add(0,converter);//返回是string的话,默认把这个放在最前,否则ResponseAdvisor 处理字符串返回时会报类型不一致
    }
}

五、Failed to instantiate xxx

自建bean后,启动项目时有时会报bean找不到,或者初始化不成功,导致项目启动失败。原因很简单,是因为自建的bean没有默认构造方法;springboot管理所有bean都需要默认的构造方法,也就是无参构造方法,手动加上后问题就会解决。