【问题标题】:Simple restful webservice example with multiple @get and @put带有多个@get 和@put 的简单的restful web 服务示例
【发布时间】:2012-04-24 08:42:42
【问题描述】:

尊敬的 Stackoverflow 用户, 谁能给我一个简单的例子,在netbeans中有2个或更多@get和@put的restful webservice?任何简单的纯文本示例就足够了。它不应该包括任何数据库! 如果有人可以帮助我,我会非常高兴。 我尝试了一些方法,但没有成功,这是代码

@Path("/simple")
public class SimpleResource_1 {
    @Context
    private UriInfo context;
    @GET
    @Produces("text/plain")
    public String getText() {
        return "hello world";
    }
@Path("/simple/simple1")
public class SimpleResource_11 {

    @Context
    private UriInfo context;
@GET
@Produces("text/plain")
public String getText1(){
    return "hi";
}
}

此代码出现运行时错误,但没有任何编译错误。 我做对了吗?还是我错过了什么!!

提前致谢!

干杯!

【问题讨论】:

  • @MarioDeSchaepmeester 我使用 javax.ws,谢谢回复。

标签: web-services http rest get


【解决方案1】:

假设您使用的是默认的 Netbeans 服务器堆栈,因此使用的是javax.ws.rs

@Path("/admin/")
public class AdminResource {

    @GET
    @Produces('text/plain')
    @Path("/reset/")
    public String reset() {
        return "Reset successfully";
    }

    @GET
    @Produces('text/plain')
    @Path("/close/")
    public String closeAll() {
        return "Closed Successfully";
    }
}

每个 Java 文件应该只有一个公共类。如果您创建了标准 Web 服务项目,Netbeans 应该为上述代码整理所需的导入。如果您想添加@PUT,您只需向资源添加另一个方法并对其进行适当的注释。

【讨论】:

  • 非常感谢您的回复,但是当我在我的 netbeans 中添加此代码时,我收到一个错误“注释类型不适用于此类应用程序”
  • 任何帮助将不胜感激?
  • 谢谢伙计,我想通了,问题出在第二次进入生产线。非常感谢你,伙计。但是如何实现put呢?和get类似吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
  • 2017-06-07
  • 1970-01-01
相关资源
最近更新 更多