官术网_书友最值得收藏!

Sample REST program

We will use a simple approach to building a standalone application. It packages everything into a single executable JAR file, driven by a main() method. Along the way, you use Spring's support for embedding the Jetty servlet container as the HTTP runtime, instead of deploying it to an external instance. Therefore, we would create the executable JAR file in place of the war that needs to be deployed on external web servers, which is a part of the rest module. We'll define the domain models in the lib module and API related classes in the rest module.

The following are pom.xml of the lib and rest modules.

The pom.xml file of the lib module:

<?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> 
    <parent> 
        <groupId>com.packtpub.mmj</groupId> 
        <artifactId>6392_chapter2</artifactId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
    <artifactId>lib</artifactId> 
</project> 

The pom.xml of the rest module:

    <modelVersion>4.0.0</modelVersion> 
    <parent> 
        <groupId>com.packtpub.mmj</groupId> 
        <artifactId>6392_chapter2</artifactId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
    <artifactId>rest</artifactId> 
  <dependencies> 
        <dependency> 
            <groupId>com.packtpub.mmj</groupId> 
            <artifactId>lib</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
    ... 
    ...  

Here, the spring-boot-starter-web dependency is used for developing the standalone executable REST service.

We'll add the following module-info.java classes in the lib and rest modules in their default package, respectively.

The module-info.java file in the lib module:

module com.packtpub.mmj.lib { 
    exports com.packtpub.mmj.lib.model to com.packtpub.mmj.rest; 
    opens com.packtpub.mmj.lib.model; 
} 

Here, we are exporting the com.packtpub.mmj.lib.model package to com.packtpub.mmj.rest, which allows access of the lib model classes to the rest module classes.

The module-info.java file in the lib module:

module com.packtpub.mmj.rest { 
 
    requires spring.core; 
    requires spring.beans; 
    requires spring.context; 
    requires spring.aop; 
    requires spring.web; 
    requires spring.expression; 
 
    requires spring.boot; 
    requires spring.boot.autoconfigure; 
 
    requires com.packtpub.mmj.lib; 
 
    exports com.packtpub.mmj.rest; 
    exports com.packtpub.mmj.rest.resources; 
 
    opens com.packtpub.mmj.rest; 
    opens com.packtpub.mmj.rest.resources; 
} 

Here, we're adding all the required spring and lib packages using the requires statement, which enables the rest module classes to use classes defined in the spring and lib modules. Also, we're exporting the com.packt.mmj.rest and com.packt.mmj.rest.resources packages.

Now, as you are ready with Spring Boot in NetBeans IDE, you could create your sample web service. You will create a Math API that performs simple calculations and generates the result as JSON.

Let's discuss how we can call and get responses from REST services.

The service will handle the GET requests for /calculation/sqrt or /calculation/power and so on. The GET request should return a 200 OK response with JSON in the body that represents the square root of a given number. It should look something like this:

{ 
  "function": "sqrt", 
  "input": [ 
    "144" 
  ], 
  "output": [ 
    "12.0" 
  ] 
} 

The input field is the input parameter for the square root function, and the content is the textual representation of the result.

You could create a resource representation class to model the representation by using Plain Old Java Object (POJO) with fields, constructors, setters, and getters for the input, output, and function data. Since it is a model, we'll create it in the lib module:

package com.packtpub.mmj.lib.model; 
 
import java.util.List; 
 
public class Calculation { 
 
    String function; 
    private List<String> input; 
    private List<String> output; 
 
    public Calculation(List<String> input, List<String> output, String function) { 
        this.function = function; 
        this.input = input; 
        this.output = output; 
    } 
 
    public List<String> getInput() { 
        return input; 
    } 
 
    public void setInput(List<String> input) { 
        this.input = input; 
    } 
 
    public List<String> getOutput() { 
        return output; 
    } 
 
    public void setOutput(List<String> output) { 
        this.output = output; 
    } 
 
    public String getFunction() { 
        return function; 
    } 
 
    public void setFunction(String function) { 
        this.function = function; 
    } 
 
} 
主站蜘蛛池模板: 宝清县| 邢台市| 普兰店市| 太仓市| 台东县| 广河县| 赤峰市| 洛浦县| 邵阳市| 浑源县| 贵德县| 西乡县| 漳平市| 隆德县| 都江堰市| 双峰县| 南宁市| 五莲县| 获嘉县| 东乌| 无锡市| 庐江县| 广德县| 安阳市| 巴马| 昌宁县| 元江| 东乡族自治县| 宝兴县| 临沂市| 富蕴县| 柘荣县| 特克斯县| 祁门县| 吴堡县| 彭州市| 西青区| 漳州市| 吉安市| 高阳县| 清丰县|