Spring boot学习笔记 实践教程https://spring.io/guides/gs/rest-service/

来源:互联网 发布:怎么提升淘宝买家等级 编辑:程序博客网 时间:2024/05/18 02:08

网上的教程,拿来实现一下:源地址 https://spring.io/guides/gs/rest-service/

前提 IDE得有,jdk1.8以上,maven工具


最终目标:

浏览器访问:http://localhost:8080/greeting 返回:{"id":1,"content":"Hello, World!"}

带参数访问:http://localhost:8080/greeting?name=User  返回 {"id":1,"content":"Hello, User!"}


从链接 https://spring.io/guides/gs/rest-service/ 右侧,下载code,放到我的项目目录中 得到两个文件,

其中

initial是原始的,我们打开这个来操作。 complete是完成之后的效果,方便我们运行看效果以及后期代码比对。


打开项目:选择initial对应文件夹中的pom文件即可。

基本框架导入进来来写java代码,用以响应浏览器中对 /greeeting路径 的访问

【以下代码都在 src/main/java/hello这个package里边】

新建java文件:src/main/java/hello/Greeting.java

新建好之后是空白的

补充代码:

因为要返回一个id,和一个content,就按照网站上的代码,敲进去就可以了

不要加中文注释,以防出现编码不兼容,需要注释就用英文或拼音

package hello;/** * Created by on 2017/5/10. */public class Greeting {    private final long id;    private final String content;    public Greeting(long id, String content) {        this.id = id;        this.content = content;    }    public long getId() {        return id;    }    public String getContent() {        return content;    }}

为了响应前台请求,需要建立controller类

src/main/java/hello/GreetingController.java

package hello;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.atomic.AtomicLong;/** * Created by on 2017/5/10. */@RestControllerpublic class GreetingController {    private static final String template = "Hello, %s!";    private final AtomicLong counter = new AtomicLong();    @RequestMapping("/greeting")    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {        return new Greeting(counter.incrementAndGet(),                String.format(template, name));    }}
上边中的  @RequestMapping("/greeting") 就代表前端进入greeting路径,就会进入这个方法来处理

The @RequestMapping annotation ensures that HTTP requests to /greeting are mapped to the greeting() method.

下边方法   @RequestParam 是对参数的要求,要求参数名为name,如果无参数,则默认值为World          接收参数类型为  String name

然后方法会新建一个 Greeting对象,并返回   id使用了一个AtomicLong类并调用get方法获取id值,content则是直接将name返回,以 “Hello, %s!”这种样式返回

(因为都是在package包中,所以不需要单独导入Greeting类,就能直接使用)

@RequestParam binds the value of the query string parameter name into the name parameter of the greeting() method. This query string parameter is explicitly marked as optional (required=true by default): if it is absent in the request, the defaultValue of "World" is used.

The implementation of the method body creates and returns a new Greeting object with idand content attributes based on the next value from the counter, and formats the given name by using the greeting template.


但是到现在,还没法直接运行,需要建立一个应用程序启动的main函数

src/main/java/hello/Application.java

package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by ZhangHongchen on 2017/5/10. */@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class,args)    }}

【原网站解释

@SpringBootApplication is a convenience annotation that adds all of the following:@Configuration tags the class as a source of bean definitions for the application context.@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.


右键刚才的类Application.java,选择  run application

(可以看到IDE左边的目录中,多了target文件目录(这就是启动应用产生的可执行文件,感觉),然后Idea自带的tomcat启动成功)



试试带参数访问?每次运行,id都会自动加一

http://localhost:8080/greeting?hi=FirstSpringWebapp&name=中国
可以看到是能正常返回的,且由于name不是必填参数且有默认值,所以我们带其它参数一起访问也能返回

【粘贴的图片都没上来。。下次编辑得多注意】

1 0
原创粉丝点击