spring boot最小web项目

创建一个spring boot最小web项目,除了一个pom文件配置,不需要任何其他xml之类的配置了

这源于spring boot的一个核心价值,即约定大于配置

开发人员不需要告诉应用,这个地方要如何如何,那个地方要如何如何,一切尽在不言中,正如那个名言——你懂的

你只需要给出很少量的暗示,剩下的就交给spring boot去处理吧


但是任何事物都有两面,spring boot也是一把双刃剑,你懂的,这其中的深刻含义,意味着spring boot会按照它的习惯,或者说是默认方式去替你配置本应该由你来完成的一系列操作,一旦这个默认配置不是你所想要的,你还是得亲自上手去干预,这时候你会发现程序似乎正在脱离你的掌控,你的权力正在削弱

但也必须得承认,spring boot在它的领域确实做得很好,它可以帮助你在数分钟之内成功运行一个最小web项目。

唯一的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>

    <groupId>spring-boot-helloworld</groupId>
    <artifactId>spring-boot-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable JAR -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Allow access to Spring milestones and snapshots -->
    <!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
    
</project>


在加上三个java文件

1.main函数


package org.spring.my.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends SpringBootServletInitializer implements
		EmbeddedServletContainerCustomizer {

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

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

	/*
	 * (non-Javadoc) 通过实现EmbeddedServletContainerCustomizer接口设置Tomcat服务端口
	 * 
	 * @see
	 * org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer
	 * #customize(org.springframework.boot.context.embedded.
	 * ConfigurableEmbeddedServletContainerFactory)
	 */
	public void customize(ConfigurableEmbeddedServletContainerFactory arg0) {
		// TODO Auto-generated method stub
		arg0.setPort(8081);
	}
}

2.控制器
<pre name="code" class="java">package org.spring.my.boot.controller;

import org.spring.my.boot.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class UserController {

	@RequestMapping("/{id}")
	private User view(@PathVariable("id") Long id) {
		User user = new User();
		user.setId(id);
		user.setName("xxx");
		return user;
	}

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



 
3.实体

<pre name="code" class="java">package org.spring.my.boot.entity;

import java.io.Serializable;

/**
 */
public class User implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}


 

运行main函数,spring boot会自动将服务以Restful风格的web接口发布在嵌入式Tomcat服务器的8081端口

在浏览器中输入http://localhost:8081/user/1,即可得到实体以json格式返回



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值