当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Jax-rs规范下REST接口使用方法详解

Jax-rs规范下REST接口使用方法详解

2020年09月10日  | 移动技术网IT编程  | 我要评论
rest接口目前有2种方式: 一种是遵循了jax-rs规范的,使用的是@path,@pathparam等注解.一种是spring自有的一套,使用的是@restcontroller,@getmappin

rest接口目前有2种方式: 一种是遵循了jax-rs规范的,使用的是@path,@pathparam等注解.一种是spring自有的一套,使用的是@restcontroller,@getmapping等注解.

如何开发jax-rs规范的rest接口呢?

在springboot项目中,可以按如下步骤进行:

1. 引入依赖: spring-boot-starter-jersey

2. 添加配置:

public class jerseyconfig extends resourceconfig{
  public jerseycongfig(){
    super.register(jax-rs规范的rest接口所在实现类.class);
  }
}

3. 具体使用:

  有2种方式,一种是简单的直接使用实现类作为接口所在类,一种是接口与实现类搭档的方式.

  3.1 直接以实现类作为接口所在类的方式与spring的controller类似,只不过改为使用jax-rs规范的@path,@pathparam注解

  @path("/user")
  @produces("application/json")
  public class mycontroller{
    @path("/getname/{name}")
    @get
    public string getname(@pathparam("name")string name){
      return name;
    }
  }

  3.2 以接口与实现类搭档的方式

  接口:

  @path("/user")
  @produces("application/json")
  public interface imycontrollerservice {
    @path("/getname/{name}")
    @get
    string getname(@pathparam("name")string name);
  }

  实现类:

  public class mycontroller implements imycontrollerservice{
    public string getname(string name){
      return name;
    }
  }

4.注意点

  无论哪种使用方式,都要将实现类注册到resourceconfig中,否则找不到.

  用一个对象不能接收多个path参数, 如果需要用,需要其他配置

  可以使用多个@pathparam接收多个path参数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网