A book "Java MicroServices", the first example of a successful test

Source: Internet
Author: User

Static language, Java should be more understanding, combined with micro-services, DOCKER, and then engage in spring CLOUD, you can keep up with the times.

By the chain, the address of a buy book:

Https://item.jd.com/12089180.html

(I sometimes do not understand, a programmer's income, buy some books to be so careful?) Hehe, what is the income to do?? :))

Pom.xml

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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

 PackageCom.packtpub.mmj.restsample.model;Importjava.util.List; Public classcalculation {String Funciton; PrivateList<string>input; PrivateList<string>output;  PublicCalculation (list<string> input, list<string>output, String function) {         This. Funciton =function;  This. Input =input;  This. Output =output; }     PublicString Getfunciton () {returnFunciton; }     Public voidSetfunciton (String funciton) { This. Funciton =Funciton; }     PublicList<string>GetInput () {returninput; }     Public voidSetInput (list<string>input) {         This. Input =input; }     PublicList<string>GetOutput () {returnoutput; }     Public voidSetoutput (list<string>output) {         This. Output =output; }        }

Calculationcontroller.java

 Packagecom.packtpub.mmj.restsample.resources;Importcom.packtpub.mmj.restsample.model.Calculation;Importjava.util.ArrayList;Importjava.util.List;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;Import StaticOrg.springframework.web.bind.annotation.RequestMethod.GET;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController, @RestController @requestmapping ("/calculation") Public classCalculationcontroller {Private Static FinalString PATTERN = "^-?+\\d+\\.?" +\\d*$ "; @RequestMapping ("/power")     PublicCalculation pow (@RequestParam (value= "base") String B, @RequestParam (value= "exponent") String e) {List<String> input =NewArraylist<string>();        Input.add (b);        Input.add (e); List<String> output =NewArraylist<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 NewCalculation (input, output, "power"); } @RequestMapping (Value= "/sqrt/{value:.+}", method=GET) PublicCalculation sqrt (@PathVariable (value= "value") String avalue) {List<String> input =NewArraylist<string>();        Input.add (Avalue); List<String> output =NewArraylist<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 NewCalculation (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 {    publicstaticvoid  main (string[] args) {        Springapplication.run (restsampleapplication. class , args);}    }

Output:

Java microservices, a book you can read, and test successfully the first sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.