搭建springboot环境

1.前戏准备:

  

  SpringBoot核心jar包:这里直接从Spring官网下载了1.5.9版本.

  jdk:jdk1.8.0_45.

  maven项目管理工具:3.5版本.

  tomcat:8.5版本.

  本地仓库:注意settings.xml里面的设置"<localRepository>E:/SpringBoot/repository</localRepository>"红色部分代表仓库的位置.

  eclipse:jee-neon3版本.

2.环境设置

  1)将jdk以及maven的环境变量配置好,配好后在dos窗口进行测试:命令为java -version 和 mvn -v

  JAVA_HOME:jdk所在目录

  path:%JAVA_HOME%in

  clathpath:%JAVA_HOME%lib;%JAVA_HOME%lib ools.jar

  MAVEN_HOME:maven所在目录

  path:%MAVEN_HOME%in

  2)eclipse设置

  jre替换成1.8版本:Windows-->Preferences-->java-->Installed JREs

  maven仓库及路径设置:Windows-->Preferences-->maven-->installations 新建一个maven

            :Windows-->Preferences-->maven-->user settings 路径都设为仓库的settings.xml所在路径

  server:Windows-->Preferences-->server-->RunTime Environments 添加tomcat8.5

 

3.创建简单的maven项目

  创建一个maven start项目

  pom.xml配置,红色部分为重要部分

 <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>

 

    <groupId>com.sinosoft</groupId>

    <artifactId>HelloSpring</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

 

    <name>HelloSpring</name>

    <url>http://maven.apache.org</url>

 

    <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.9.RELEASE</version>

    </parent>

    <dependencies>

      <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>3.8.1</version>

        <scope>test</scope>

      </dependency>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

    </dependencies>

 </project>

   在src/main/java下新建一个Controller类

  package com.sinosoft.HelloSpring;

   import java.io.IOException;

   import javax.servlet.http.HttpServletResponse;

   import org.springframework.boot.SpringApplication;

   import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

 

   @RestController

   @SpringBootApplication

   public class HelloSpring {

@RequestMapping("/app")

void index(HttpServletResponse res) throws IOException{

res.getWriter().println("Hello World!");

}

public static void main(String[] args) {

SpringApplication.run(HelloSpring.class, args);

}

  }

 运行main方法,在浏览器中输入http://localhost:8080/app就能得到 Hello World! 的结果

4.浏览器差异可能导致的问题

  最开始用的IE8浏览器,代码及报错如下:

  @RestController

   @SpringBootApplication

   public class HelloSpring {

@RequestMapping("/app")

String index(){

   return "Hello World!";

}

public static void main(String[] args) {

SpringApplication.run(HelloSpring.class, args);

}

  }

  

  可是我在360浏览器上却可以正常运行并出结果,后来经查询资料,得到这是ie8不支持

 所以将红色部分方法替换为

  void index(HttpServletResponse res) throws IOException{

res.getWriter().println("Hello World!");

    }

  便可在ie8上完美运行

这只是个开始...

原文地址:https://www.cnblogs.com/goujh/p/8394033.html