创建一个接受POST和GET的Jax-RS RESTful服务? [英] create a Jax-RS RESTful service that accepts both POST and GET?

查看:486
本文介绍了创建一个接受POST和GET的Jax-RS RESTful服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我现有的一项服务转换为RESTful,而且我已经掌握了与RestEasy协同工作的基本知识。我的一些客户端应用程序应该能够对多个服务执行GET和POST请求。我只是想找到一个简单的方法来解决jax-rs指定API应该同时接受GET和POST的问题。下面你可以找到一个测试方法,如果你看到任何方法而没有在@GET和@QueryParam的另一个类中复制它,请告诉我。

I'm converting one of my existing service to become RESTful and I've got the basic things working with RestEasy. Some of my client apps should be able to execute both GET and POST requests to several services. I'm just seeking if there is any easy way around jax-rs to specify that API should accept both GETs and POSTs. Following you can find a test method, let me know if you see any way around without duplicating this in another class with @GET and @QueryParam.

@POST
@Path("/add")
public Response testREST(@FormParam("paraA") String paraA,
        @FormParam("paraB")  int paraB) {

    return Response.status(200)
            .entity("Test my input : " + paraA + ", age : " + paraB)
            .build();

}


推荐答案

As < a href =http://en.wikipedia.org/wiki/Representational_state_transfer#RESTful_web_services>维基百科说,如果API是具有四个定义方面的资源集合,则它是RESTful:

As wikipedia says, an API is RESTful if it is a collection of resources with four defined aspects:


  • Web服务的基本URI,例如 http://example.com/resources/

  • Web服务支持的数据的Internet媒体类型。这通常是XML,但可以是任何其他有效的Internet媒体类型,前提是它是有效的超文本标准。

  • Web服务使用HTTP方法支持的操作集(例如,GET, PUT,POST或DELETE)。

  • API必须是超文本驱动的。

  • the base URI for the web service, such as http://example.com/resources/
  • the Internet media type of the data supported by the web service. This is often XML but can be any other valid Internet media type providing that it is a valid hypertext standard.
  • the set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE).
  • The API must be hypertext driven.

By减少 GET POST 之间的差异,你违反了第三方面。

By diminishing the difference between GET and POST you're violating the third aspect.

这篇关于创建一个接受POST和GET的Jax-RS RESTful服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆