java入门笔记三(springboot框架REST接口)

1 打开eclipse, File->New->Maven Project,然后有个archetype(原型)选择,可以不选,

 填写一下Groupid和Artifactid完点击完成即可。

2 修改项目pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.meng</groupId>
  <artifactId>springBootRest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

	
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
</dependencies>

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
</project>

3 项目右键maven->Update Project更新

4 项目src/main/java下面新增包和类

package springBootRest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*
 * springboot启动类(引导类)
 */

@SpringBootApplication //表明当前类是springboot的引导类
public class Application {
    
    public static void main(String[] args) {
        System.out.println("启动springboot");
        SpringApplication.run(Application.class, args);
    }
}

5 编译运行报错 java.lang.UnsupportedClassVersionError: javax/annotation/ManagedBean

版本的问题,选中项目修改JRE Systerm Library版本改成1.8重新编译即可

6 添加一个方法

package springBootRest.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("hello2")

public class Hello{

       @RequestMapping("")
       public String hello() {

              return "helloworld2";

       }

}

然后http://localhost:8081/hello2即可访问

7 端口被占用处理 java.net.BindException: Address already in use

lsof -i tcp:port

kill -9 pid

 

posted @ 2021-02-06 00:54  阿拉蕾家的小铁匠  阅读(137)  评论(0编辑  收藏  举报