【web】myeclipse+spring boot+maven之helloworld

这里介绍使用myeclipse2014结合maven开发spring boot 的helloworld应用处理方式。

1、新建工程

打开myeclipse2014 ide,新建一个maven工程,详细步骤可参考下图所示:

 2、修改maven配置文件pom.xml

打开工程中生成的pom.xml文件,修改默认配置。如下直接上我的配置,具体说明自行百度或者后面再补充。

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

  <groupId>com.xiaobuke</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url> 

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  
  <parent>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-parent</artifactId>
	  <version>1.1.4.RELEASE</version>
  </parent>

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  </dependencies>
    <!--maven的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        
    <!-- 配置java版本 不配置的话默认父类配置的是1.6-->
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3、增加启动类

示例中新建Application类作为启动类,该类在项目的包根目录下(我这里打开另外一个示例工程,所以包名是com.xiaobuke.dbms,请注意匹配),该类的作用很简单:

  1. 启动自动配置;
  2. 开启组件扫描;
  3. 提供入口函数main函数;

直接上内容:

package com.xiaobuke.dbms;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
	
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

4、增加controller

这里增加一个非常简单的restcontroller用来进行测试,该controller非常简单,实现如下功能:

  1. 映射/test的url到项目;
  2. 当使用get方法请求url时直接返回"hello,world"字样;

直接上代码(该类放到了controller的包下):

package com.xiaobuke.dbms.controller;

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

@RestController
@RequestMapping("/test")
public class TestController {
	
	@RequestMapping(method=RequestMethod.GET)
	public String test() {
		return "hello,world";
	}

}

5、增加工程配置

新建一个prop文件,将工程的一些相关配置放到该prop文件中方便修改使用。示例中配置文件名为application.properties,存放在src---main---resources目录下。文件的内容比较初级,直接附上:

server.port=8081
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8
 
 
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false

6、运行测试

运行测试时非常简单,直接选中Application.java类,然后右键run as----java application运行调试即可。如下图所示:

运行 起来后的一些效果:

此时可以打开浏览器,然后输入测试连接(http://localhost:8081/test)进行测试。 如下为本文测试结果:

7、打包发布

本文使用maven进行打包发布。打开命令行窗口并进入到pom.xml文件所在的目录,执行如下命令进行编译:

mvn package

打包成功后输出内容如下:

此时 可以在target目录下看到编译打包后生成的jar文件。可以在jar包所在目录下执行如下命令进行简单的运行测试:

java -jar dbms-0.0.1-SNAPSHOT.jar

运行后打开浏览器访问测试url测试即可。

 

代码下载地址:https://download.csdn.net/download/yxtouch/10748291

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值