idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。

一、idea 使用spring initializr不选择web搭建springboot项目

1.file => new => project

2.直接next到finish结束。

3.完成后创建controller用例测试

4.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cc.ash</groupId>
    <artifactId>boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

        <!--  手动添加web依赖,@restController @requestMapping注解等      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.application.properties

#端口号修改。默认8080
server.port=

6.springboot入口启动类

package cc.ash.bootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class BootDemoApplication {


    @RequestMapping("/boot")
    @ResponseBody
    public String bootTest() {

        return "hello springboot";
    }
    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class, args);
    }

}

7.新建包下controller测试类

package cc.ash.bootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(name="springboot test", value = "/test")
    public String test() {
        return "booted";
    }
}

8.运行结果

新建包下controller测试类:正常

springboot启动类中访问404。改requestMapping、rerun,做了很多操作到怀疑人生。重启idea,世界终于正常了。

能正常访问的结果如下

二、maven搭建springboot项目

1.jdk7的情况下

2.maven搭建

3.手动添加pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<groupId>cc.ash</groupId>
<artifactId>boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<java.version>1.8</java.version>
</properties>

<!-- =================jdk1.7与springboot版本不兼容======================= -->
<!--<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.myproject.Application</start-class>
</properties>-->

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

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

<!-- 手动添加web依赖,@restController @requestMapping注解等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

maven导入依赖(未选择自动导入的情况下)。

4.编写测试用例代码

1).测试controller:

  TestController

package cc.ash.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(name="maven手动创建springboot",value={"/manual", "mvnManual"})
    @ResponseBody
    public String test() {

        return "maven manual";
    }

}

2.启动入口:

DemoApplication
package cc.ash;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

3.完成后代码结构

5.访问测试

6.jdk1.7错误留存

不兼容问题

https://blog.csdn.net/wj197927/article/details/79557017

https://www.jianshu.com/p/95d8a0cf0244

idea日志记录(非控制台日志,控制台日志如上)

猜你喜欢

转载自www.cnblogs.com/foolash/p/11747488.html