SpringBoot学习-第一个Spring Rest应用

                                第一个Spring Rest应用

1 创建Marven工程

         (1)新建Marven工程

             可以使用https://start.spring.io/ 网站来快速的生成一个spring boot的模板程序,包含了spring boot的基本结构或者使用下        面的新建工程向导

 

       选择新建工程的Marven选项,archetype可以选择默认点击下一步

      

      设置下项目的GroupID,ArtifactID,Version 

      

      (2)工程结构

       工程结构图如下,工程目录主要包括src程序目录,resources 资源目录其中在资源目录中新建application.properties程序默认配置文件,pom.xml 配置依赖的库文件 由于使用了maven 保持src下main和test的目录结构

 2   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.hisense</groupId>
    <artifactId>SpringbootPractice1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--先加载父模块pom的配置 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

   <!--spring-boot-starter-web添加了Tomcat和Spring MVC -->
   <!-- - spring-boot-starter 模板:核心模块,包括自动配置支持、日志和YAML
     - spring-boot-starter-web 模块:web模块-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <!--定义属性可以在任何位置使用${java.version} 的方式的到值1.8-->
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

     spring-boot-starter-web添加了Tomcat和Spring MVC 
     spring-boot-starter 模板:核心模块,包括自动配置支持、日志和YAML
     spring-boot-starter-web 模块:web模块

 

对于copy过来的pom.xml依赖文件,默认系统不会再自动下载需要的类库,点击idea->视图->工具窗口->marven工程菜单打开marven Project 窗口,点击刷新按钮便可下载依赖的库文件

   

2 创建访问控制器

       在src目录中创建文件GreetingController.java ,作为web访问的资源控制器处理get请求, @RestController 注解类表明此类实现SpringBoot的web rest访问功能,类中的Greeting函数使用了注解 @RequestMapping("/greeting") 代表可以使用 “网址/greeting” 的形式访问此函数,返回的类Greeting 会被SpringBoot自动序列号为json字符串返回给调用者。

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

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {

        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}

    Greeting函数没有明确GET vs. PUTPOST ,所有的对/greeting的请求都会进入此方法,因为@RequestMapping 映射所有的http操作, 可以使用@RequestMapping(method=GET)  to 明确get映射

@RestController注解 等价于 @Controller+@ResponseBody 将返回值已json形式返回

Spring’s MappingJackson2HttpMessageConverter 会自动被选择 转换 Greeting到JSON.

  其中使用的Greeting类定义为

package hisense;
public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

3 浏览器测试

    在src目录下创建HelloBoot.java 程序入口类

 注意:Application启动类文件一定要放在包的最外侧,而且必须放在包下面,不允许直接的根目录,从application路径下可以搜索到所有的子包 
 原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.例如一下的结构

package hisense;

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

@SpringBootApplication
public class HelloBoot {

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

    }
}

@SpringBootApplication注解类方便的自动添加了下面所有的注解:

  • @Configuration  tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.

  • @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.

运行程序后

  在浏览器三种输入localhost:8080/greeting 或者 http://localhost:8080/greeting?name=User 使用get 请求访问rest接口 /greeting,在访问网址中可以带用name参数

  可以看到如下结果

  {"id":1,"content":"Hello, World!"}

4 修改内置Web服务器的端口

 

    修改application.properties

      我们只需要在在资源文件夹resources内创建 application.properties配置文件

      在里面加入    server.port=8004

server.port=8081

  这样便可以使用localhost:8004/greeting来进行访问了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值