记录一次springboot的使用

使用springboot开发接口,并调用第三方接口。
1.MD5的加密,要考虑JVM编码格式,UTF-8和GBK的环境,经过MD5加密,得到的东西是不一样的。而且MD5两边最好统一,不同的参数会导致不同结果。
2.Windows服务器部署项目,有两种方法,一种是外置的Tomcat,一种是jar启动(Springboot项目)。外置tomcat启动的时候,编码格式也要注意,改变jvm编码,可以在服务器的计算机属性(和配置java一个地方)里配置。下面的代码可以查看JVM编码:

		System.out.println(System.getProperty("file.encoding"));

        try {
            Properties properties=System.getProperties();
            PrintWriter out=null;
            out = new PrintWriter(new File("a.txt"));
            properties.list(out);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

当使用springcloud的时候,外置tomcat要靠编辑conf文件夹下的server.xml来改变端口号(3个端口都要改,否则报错),而且外置tomcat启动时,要把webapps/ROOT文件夹删除,然后把自己打包的war改名为ROOT.war(这点和springboot不一样)。内置tomcat启动,即jar启动时,端口号在idea里修改yml就可以了。

3.MongoDB的版本问题,会导致返回值的不同。
mongoTemplate.getCollection(statementName)在1的版本,返回值类型是DBCollection ,但是后来的版本返回值类型是MongoCollection< Document>,
DBCollection collection = (DBCollection) mongoTemplate.getCollection(statementName);
这样是不行强转的,会报错。应该使用
MongoCollection< Document> collection = mongoTemplate.getCollection(statementName);但是这样collection的方法没有save,我们要把
DBObject dbObject = new BasicDBObject(); dbObject.putAll(map);
改为

		Document document=new Document();
        document.putAll(map);

        collection.insertOne(document);

4.监听器的使用
一开始使用@WebListener不起作用,不知道为什么,后来使用下面代码可以:

@Component

public class MyListener1 implements ApplicationListener<ApplicationReadyEvent> {

    private Logger logger = LoggerFactory.getLogger(MyListener1.class);

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        SpringApplication app = event.getSpringApplication();

		要使用的代码。。。。。。。。。。。。
          
    }
}

5.springboot使用外置tomcat,要在启动类继承SpringBootServletInitializer并重写SpringApplicationBuilder configure方法:

public class ProviderApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ProviderApplication.class);
    }
}

6.日志文件使用配置文件输出到指定文件夹比较方便,编码也可以设置,具体配置在另外一篇文章
7.定时任务有配置文件和注解的方法,哪种方便用哪个,注解方式另外一篇文章有
8.http接口接收参数,可以使用

@Context HttpServletRequest request, @Context HttpServletResponse response

也可以使用

@RequestBody JSONObject jsonObject

9.同一个版本的gson出现问题,原因不清楚,后来用阿里json代替了方法。原来代码 是:

map = GsonUtil.fromJson(map.get("resultstr").toString(), map.getClass());

改后是:

 map=JSONObject.parseObject(map.get("resultstr").toString(), map.getClass());

10.很多文件,都是要区分编码格式的,可能你从别人那里复制文件过来就不好使了,记得尽量自己新建文件,然后复制代码
11.有第三方包的时候,jar和war打包,pom的配置是不一样的,参考https://blog.csdn.net/dhj393338/article/details/86495200
我自己的配置是:

<!--打war的配置-->
    <build>
        <finalName>文件名</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                    <compilerArguments>
                        <extdirs>${project.basedir}/src/main/resources/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/lib</directory>
                            <targetPath>WEB-INF/lib/</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>


        </plugins>

    </build>

    <!--打jar的配置-->
    <!--<build>-->
        <!--<finalName>文件名</finalName>-->
        <!--<plugins>-->
            <!--<plugin>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
                <!--<configuration>-->
                    <!--<includeSystemScope>true</includeSystemScope>-->
                <!--</configuration>-->

            <!--</plugin>-->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-compiler-plugin</artifactId>-->
            <!--<version>2.3.2</version>-->
            <!--<configuration>-->
            <!--<source>1.8</source>-->
            <!--<target>1.8</target>-->
            <!--<encoding>utf-8</encoding>-->

            <!--</configuration>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</build>-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值