Restful接口开发(1):创建一个简单实例

一、Restful接口实例

通过构建一个Restful接口实例,更加直接深入了解Restful接口的开发。

二、构建一个简单实例

1.创建项目参考
https://blog.csdn.net/u010886217/article/details/85239110
2.项目结构

 
Maven项目结构
 
4.    创建第一个controller测试类:HelloworldController

package com.example.demo;

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

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/helloworld")
public class HelloworldController {
    @GetMapping
    public Map<String,Object> sayHelloworld(){
        Map<String,Object> result=new HashMap<>();
        result.put("message","hello world!");
        result.put("message2","hello world 2!");
        result.put("message3","hello world 3!");
        return result;
    }
}

运行启动类:DemoApplication


4.测试刚刚命令,访问http://localhost:8080/helloworld,返回结果
 

三、构建一个复杂实例

1. DTO类:TvSeriesDto.class,功能:传输数据电视剧系列的对象

package com.example.demo;

import com.sun.xml.internal.ws.spi.db.DatabindingException;

import java.util.Date;

public class TvSeriesDto {
    private int id;
    private String name;
    private int seasonCount;
    private Date originRelease;

    //构造函数
    public TvSeriesDto(){

    }
    public TvSeriesDto(int id,String name,int seasonCount,Date originRelease){
        this.id=id;
        this.name=name;
        this.seasonCount=seasonCount;
        this.originRelease=originRelease;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSeasonCount() {
        return seasonCount;
    }

    public void setSeasonCount(int seasonCount) {
        this.seasonCount = seasonCount;
    }

    public Date getOriginRelease() {
        return originRelease;
    }

    public void setOriginRelease(Date originRelease) {
        this.originRelease = originRelease;
    }
}

2.    调用类

package com.example.demo;

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

import java.util.*;

@RestController
public class HelloworldController {
//    @GetMapping
    @RequestMapping("/helloworld")
    public Map<String,Object> sayHelloworld(){
        Map<String,Object> result=new HashMap<>();
        result.put("message","hello world!");
        result.put("message2","hello world 2!");
        result.put("message3","hello world 3!");
        return result;
    }

    /**
     * 获取所有电视节目列表
     * @return
     */
    @RequestMapping("/getall")
    public List<TvSeriesDto> getAll(){

        List<TvSeriesDto> list=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        calendar.set(2016,Calendar.OCTOBER,12,0,0);
        list.add( new TvSeriesDto(1,"WestWorld",1,calendar.getTime()));
        return list;
    }

}

运行程序DemoApplication,访问http://localhost:8080/getall

3.    修改日期格式(全局)
修改..\resources\application.properties文件为..\resources\application.yml,然后添加

spring:
  jackson:
    date-format: yyyy-MM-dd #如果使用字符串型表示,用这行设置
    timezone: GMT+8
    serialization:
      write-dates-as-timestamps: false #使用数值timestamp表示日期,false表示不用这数字;true表示用数字

重新访问接口http://localhost:8080/getall,则原始数据:
[{"id":1,"name":"WestWorld","seasonCount":1,"originRelease":1476201605562}]
当前数据:
[{"id":1,"name":"WestWorld","seasonCount":1,"originRelease":"2016-10-11"}]

注意yml文件格式

(1)    每行缩进是两个空格
(2)    冒号后面要有一个空格

(至此,一个基于springboot框架的初始项目构建好了)

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C#中开发RESTful API接口可以按照以下步骤进行: 1. 首先,创建一个类来定义数据模型。这个类需要使用DataContract特性,并定义一些属性来表示数据字段。例如,可以创建一个名为Student的类,并在其中定义Name和Age属性。 2. 接下来,在接口文件中创建一个接口,并使用ServiceContract特性来标记这个接口。这个接口将包含所有的API方法。可以在其中定义一些HTTP动词(如GET、POST、PUT、DELETE)和相应的方法签名。 3. 在实现接口的类中,编写具体的方法来处理API请求。可以在这些方法中进行数据的增删改查等操作。在方法中可以使用WebGet和WebInvoke特性来标记对应的HTTP动词和URL路径。 4. 在主窗体中,创建一个服务宿主ServiceHost来承载RESTful API。可以使用WebServiceHost类,并在构造函数中指定服务实例和服务的地址。在窗体加载时打开服务,在窗体关闭时关闭服务。 需要引用以下命名空间: using System.ServiceModel.Web; // 用于创建WebServiceHost using System.Runtime.Serialization; // 用于创建DataContract数据模型 以上就是C#中开发RESTful API接口的基本步骤。在实际开发中,还需要根据具体需求进行相应的路由配置、数据验证、错误处理等操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [RESTful API简介、实战及其Demo(C#)分享](https://blog.csdn.net/magicchz/article/details/130778860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值