随手看的一本书《java微服务》,测试成功了其中的第一个样例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随手看的一本书《java微服务》,测试成功了其中的第一个样例相关的知识,希望对你有一定的参考价值。
静态语言,JAVA应该多了解,结合微服务,DOCKER,再搞搞SPRING CLOUD,就能跟上时代了。
对了,链一个买书的地址:
https://item.jd.com/12089180.html
(我有时就不了解,一个程序员的收入,买一些书就那么小心呢?呵呵,收入都作什么去了??:))
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.packtpub.mmj.restsample</groupId> <artifactId>restsample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>restsample</name> <description>com.packtpub.mmj.restsample</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Calculation.java
package com.packtpub.mmj.restsample.model; import java.util.List; public class Calculation { String funciton; private List<String> input; private List<String> output; public Calculation(List<String> input, List<String> output, String function) { this.funciton = function; this.input = input; this.output = output; } public String getFunciton() { return funciton; } public void setFunciton(String funciton) { this.funciton = funciton; } 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; } }
CalculationController.java
package com.packtpub.mmj.restsample.resources; import com.packtpub.mmj.restsample.model.Calculation; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/calculation") public class CalculationController { private static final String PATTERN = "^-?+\\\\d+\\\\.?+\\\\d*$"; @RequestMapping("/power") public Calculation pow(@RequestParam(value="base") String b, @RequestParam(value="exponent") String e) { List<String> input = new ArrayList<String>(); input.add(b); input.add(e); List<String> output = new ArrayList<String>(); String powValue = ""; if (b!=null && e!=null && b.matches(PATTERN) && e.matches(PATTERN)) { powValue = String.valueOf(Math.pow(Double.valueOf(b), Double.valueOf(e))); } else { powValue = "input is error"; } output.add(powValue); return new Calculation(input, output, "power"); } @RequestMapping(value="/sqrt/{value:.+}", method=GET) public Calculation sqrt(@PathVariable(value="value") String aValue) { List<String> input = new ArrayList<String>(); input.add(aValue); List<String> output = new ArrayList<String>(); String sqrtValue = ""; if (aValue!=null && aValue.matches(PATTERN) ) { sqrtValue = String.valueOf(Math.sqrt(Double.valueOf(aValue))); } else { sqrtValue = "input is error"; } output.add(sqrtValue); return new Calculation(input, output, "sqrt"); } }
RestsampleApplication.java
package com.packtpub.mmj.restsample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestsampleApplication { public static void main(String[] args) { SpringApplication.run(RestsampleApplication.class, args); } }
输出:
以上是关于随手看的一本书《java微服务》,测试成功了其中的第一个样例的主要内容,如果未能解决你的问题,请参考以下文章