简体   繁体   English

什么是JSON REST接口

[英]what is JSON REST interface

Is there any tutorial on JSON RESTful interface (using JAVA servlet ) ? 是否有关于JSON RESTful接口的教程(使用JAVA servlet )? The purpose is to call to external REST interface for data, and handle the data by the client(javascript client). 目的是调用外部REST接口获取数据,并由客户端(javascript客户端)处理数据。

I am not sure what kind of thing is exactly JSON REST interface in JAVA... I need some tutorials to start to learn, so...I ask here. 我不确定JAVA中JSON REST界面究竟是什么类型的东西...我需要一些教程才能开始学习,所以...我在这里问。

JSON is a light-weight data serialization format based on a subset of JavaScript. JSON是一种基于JavaScript子集的轻量级数据序列化格式。

A RESTful interface is one that conforms to the constraints and characteristics of the REST architectural style . RESTful接口是符合REST架构风格的约束和特征的接口。

So, combining the two, a JSON RESTful interface is one that follows the REST architectural style and uses JSON as its data representation format (typically the content type application/json ). 因此,结合这两者,JSON RESTful接口是遵循REST架构风格并使用JSON作为其数据表示格式(通常是内容类型application/json )的接口。

To implement a service of this kind in Java there are frameworks that can help you, such as Jersey or RESTEasy . 要在Java中实现此类服务,可以使用可以帮助您的框架,例如JerseyRESTEasy Both offer additional components that support JSON (for both incoming and outgoing data). 两者都提供了支持JSON的附加组件(用于传入和传出数据)。

EDIT: 编辑:

Both Jersey and RESTEasy implement the JAX-RS spec, so it's possible to use either as a 'pure' Java EE way of doing things. Jersey和RESTEasy都实现了JAX-RS规范,因此可以将它们用作“纯粹的”Java EE处理方式。 If you want to use only the Servlet part of Java EE to do this, it's possible but you'll have to do things like parsing path/template parameters from the URI yourself. 如果您只想使用Java EE的Servlet部分来执行此操作,则可能需要执行此操作,例如自己从URI解析路径/模板参数。

You may find it difficult to use servlet-mapping elements to describe your resource URLs, and this might result in you implementing something that looks very much like JAX-RS (if you end up with one controller servlet that parses the URI and dispatches the request to another object). 您可能会发现很难使用servlet-mapping元素来描述资源URL,这可能会导致您实现与JAX-RS非常相似的内容(如果您最终得到一个解析URI并调度请求的控制器servlet)到另一个对象)。

I'd suggest reading a lot more on REST before you decide how to implement this. 在你决定如何实现之前,我建议你阅读更多有关REST的内容。 Here's some pointers: 这里有一些指示:

  • Communication between client and server should be stateless . 客户端和服务器之间的通信应该是无状态的 Avoid the HttpSession . 避免使用HttpSession
  • REST is resource-centric, not operation-centric (like RPC). REST是以资源为中心的,而不是以操作为中心的(如RPC)。 Think of the resources that your service exposes and give these URIs. 考虑您的服务公开的资源并提供这些URI。
  • REST resources are manipulated through a common interface. REST资源通过公共接口进行操作。 For HTTP services, this interface is defined by the HTTP verbs (GET, POST, PUT, DELETE, etc). 对于HTTP服务,此接口由HTTP谓词(GET,POST,PUT,DELETE等)定义。 Make sure you read section 9 of RFC 2616 to understand the semantics of each verb and the rules around what they should and should not do. 请务必阅读RFC 2616的第9节,以了解每个动词的语义以及它们应该和不应该做的规则。
  • Study the response status codes and reason phrases in RFC 2616 . 研究RFC 2616中响应状态代码和原因短语 These are part of your common interface. 这些是您的通用界面的一部分。
  • RESTful services return representations of resources. RESTful服务返回资源的表示 A representation has a content type, make sure you set this header so that clients can understand your response. 表示具有内容类型,请确保设置此标头,以便客户端可以理解您的响应。

Finally, if you go the 'pure Servlets' route, you may still find reading/creating JSON a lot easier using a parser library like Jackson . 最后,如果您使用'纯Servlets'路径,您仍然可以使用像Jackson这样的解析器库轻松读取/创建JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM