JavaEE03-SpringBoot实现REST版HelloWorld

目标

  • 接收HTTP提交
  • 返回json格式的结果

方案

用Maven管理SpringBoot项目. 例如:用NetBeans创建一个Maven类别的Java应用程序.
提示: Maven下载相关依赖的速度相当的慢. 可以通过建立本地的Maven库加速, 但是实现此技术比较复杂.

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>cn.com.subook</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

模型

Book.java

package model;

public class Book {
    private String isbn;
    private String name;

    public Book() {
    }

    public Book(String isbn, String name) {
        this.isbn = isbn;
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
}

Customer.java

package model;
public class Customer {
    private String name;
    private int age;
    private Book[] books;

    public Customer() {
    }

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }
}

控制器

RestServiceApplication1.java

package cn.com.subook.mavenproject1;

import model.Book;
import model.Customer;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class RestServiceApplication1 {

    public static void main(String[] args) {
        new SpringApplicationBuilder(RestServiceApplication1.class).web(WebApplicationType.SERVLET).run(args);
    }

    @RequestMapping(value = "/hello")
    public ResponseEntity<String> hello(String name, int age) {
        System.out.println(name + age * 2);
        String message = "<h1>Hello,World!</h1>" + name + " age is " + age;
        return new ResponseEntity<>(message, HttpStatus.OK);
    }

    @RequestMapping(value = "/hello1")
    public ResponseEntity<Customer> hello1(String name, int age) {
        Customer customer = new Customer(name + " hello1", age);
        Book[] books = new Book[2];
        for (int i = 0; i < books.length; i++) {
            books[i] = new Book("isbn000000" + i, "bookname" + i);
        }
        customer.setBooks(books);
        return new ResponseEntity<>(customer, HttpStatus.OK);
    }

    @RequestMapping(value = "/hello2")
    public ResponseEntity<Customer> hello2(Customer customer/*String name, int age*/) {
        //Customer customer = new Customer(name, age)
        customer.setName(customer.getName() + " hello2");
        Book[] books = new Book[2];
        for (int i = 0; i < books.length; i++) {
            books[i] = new Book("isbn000000" + i, "bookname" + i);
        }
        customer.setBooks(books);
        return new ResponseEntity<>(customer, HttpStatus.OK);
    }
}

编译打包

执行maven的目标package

运行

java -jar mavenproject1-1.0-SNAPSHOT.jar
运行

运行结果

hello结果

hello运行结果

hello1结果

hello1运行结果

hello2结果

hello2运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值