本文共 4721 字,大约阅读时间需要 15 分钟。
静态语言,JAVA应该多了解,结合微服务,DOCKER,再搞搞SPRING CLOUD,就能跟上时代了。
对了,链一个买书的地址:
https://item.jd.com/12089180.html
(我有时就不了解,一个程序员的收入,买一些书就那么小心呢?呵呵,收入都作什么去了??:))
pom.xml
4.0.0 com.packtpub.mmj.restsample restsample 0.0.1-SNAPSHOT jar restsample com.packtpub.mmj.restsample org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
Calculation.java
package com.packtpub.mmj.restsample.model;import java.util.List;public class Calculation { String funciton; private Listinput; private List output; public Calculation(List input, List 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 getInput() { return input; } public void setInput(List input) { this.input = input; } public List getOutput() { return output; } public void setOutput(List 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) { Listinput = new ArrayList (); input.add(b); input.add(e); List output = new ArrayList (); 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 input = new ArrayList (); input.add(aValue); List output = new ArrayList (); 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;@SpringBootApplicationpublic class RestsampleApplication { public static void main(String[] args) { SpringApplication.run(RestsampleApplication.class, args); }}
输出:
转载地址:http://qcasa.baihongyu.com/