基于SpringBoot的Restful接口开发

1、SpringBoot环境搭建

----IDE : Eclipse 文章中版本Version: Oxygen.1a Release (4.7.1a)

----JDK : jdk1.8.0_102

----MAVEN: Eclipse已含插件不需要单独安装


     1.1、Eclipse创建maven工程

     File-->New-->Maven Project-->Next-->Next


     Finish  创建完成

     1.2、编辑pom.xml,更新maven依赖

<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>restful</groupId>
	<artifactId>restful</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>restful</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<!-- Spring Boot 父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</parent>

	<dependencies>
		<!-- Spring Boot web依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project>

2、简单接口实例

     2.1、创建SpringBoot启动类

     创建maven工程时 eclipse在com.example.rest包下 默认生成了App.java,如果没有手动创建(类名无所谓)。

     修改App.java(增加注解SpringBootApplication,添加main方法)为SpringBoot启动类。

package com.example.rest;

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

/**
 * 
 * @author Administrator
 *
 */
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
     2.2、 查询用户的简单接口实现

     创建UserController.java、User.java 目录接口如下图


     User.java

package com.example.rest.inf.model;

public class User {
	private Long userId;
	private String userName;
	private String password;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
     UserController.java

package com.example.rest.inf.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.rest.inf.model.User;

@RestController
public class UserController {
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET, produces = { "application/json" })
	public User qryUser(@PathVariable(required = true, value = "userId") Long userId) {
		User user = new User();
		user.setUserId(userId);
		user.setUserName("user_"+userId.toString());
		return user;
	}
}

3、启动SpringBoot,简单测试

浏览器访问 http://127.0.0.1:8080/user/1001

输出:

{"userId":1001,"userName":"user_1001","password":null}






  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值