Spring Boot入门===Hello World

来源:互联网 发布:淘宝拍衣服多少钱一套 编辑:程序博客网 时间:2024/05/08 21:13

http://www.cnblogs.com/liangblog/p/5207855.html

     昨天无意间看到Spring Boot ,今天又了解了一下,试着写一个Hello World!  今天看了半天,最后还是要用Maven最方便!以下:

    一、工具 

       JDK1.7

       Eclipse

       Maven

       这里Eclipse集成Maven的这一步就省了!

   二、编码

      新建Maven Project 命名为:SpringBoot 选项如图

2、修改工程目录,添加源文件夹后目录如下:

 

3、修改pom.xml文件如下:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

4、编辑JAVA代码新建APP.class

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

运行此代码 服务端口默认8080  访问localhost:8080/h  展示Hello

 localhost:8080/w 展示World

OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

---------------风格线---------------------

 新建RestController风格的Controller

新建UserController

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

需修改App.java

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

运行App.java 访问 http://localhost:8080/user/12

 

新建其他Controller

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

重启程序 访问http://localhost:8080/file/name

======================================================

    修改默认端口

    一、

复制代码
package com.lgp.SpringBoot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class MainApplication implements EmbeddedServletContainerCustomizer{    @Override    public void customize(ConfigurableEmbeddedServletContainer container) {                container.setPort(8011);    }        public static void main(String[] args) {//        SpringApplication.run(MainApplication.class, args);        SpringApplication.run(FileController.class, args);    }    @RequestMapping("/main")    public String testPort(){                return "Hello 端口8011......";    }}
复制代码

   二、

复制代码
package com.lgp.SpringBoot;import java.util.concurrent.TimeUnit;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class App {      @RequestMapping("/h")      public String home() {        return "Hello";      }          @RequestMapping("/w")      public String word() {        return "World";      }          public static void main( String[] args )    {        System.out.println( "Hello World ! App!" );        //SpringApplication.run(App.class, args);        SpringApplication.run(UserController.class, args);    }        @Bean    public EmbeddedServletContainerFactory servletFactory(){        TomcatEmbeddedServletContainerFactory tomcatFactory =                 new TomcatEmbeddedServletContainerFactory();        tomcatFactory.setPort(8011);        tomcatFactory.setSessionTimeout(10,TimeUnit.SECONDS);        return tomcatFactory;            }}
复制代码

修改为8011

======================================================

0 0
原创粉丝点击