简体   繁体   English

RESTful Web服务返回类型

[英]RESTful Webservices return types

I am new to webservices in Java. 我是Java新手。 and I am writing web services using JAX-RS implementation Jersey . 并且我正在使用JAX-RS实现Jersey编写Web服务。

I'd like to know: 我想知道:

  • Which return types are possible for web service methods? Web服务方法可能有哪些返回类型?

  • As far as I know you can't use primitive types as return type for web service method.. why so? 据我所知,您不能使用原始类型作为Web服务方法的返回类型。为什么这样?

Read Chapter 8: JAX-RS Entity Providers in the Jersey docs. 阅读Jersey文档中的第8章:JAX-RS实体提供程序 It explains how (de)serialization is done in Jersey. 它说明了在Jersey中如何进行反序列化。 It explains the use of MessageBodyReader s and MessageBodyWriter s to transform the data to and from Java object to serialized streams. 它解释了如何使用MessageBodyReaderMessageBodyWriter将数据与Java对象之间的数据转换为序列化流。

At the end of the chapter, you will see the default providers that Jersey comes with out of the box 在本章的最后,您将看到开箱即用的默认提供程序。

byte[] (*/*)
String (*/*)
InputStream (*/*)
Reader (*/*)
File (*/*)
DataSource (*/*)
Source (text/xml, application/xml and media types of the form application/*+xml)
JAXBElement (text/xml, application/xml and media types of the form application/*+xml)
MultivaluedMap<K,V> (application/x-www-form-urlencoded)
Form (application/x-www-form-urlencoded)
StreamingOutput ((*/*)) - this class can be used as an lightweight MessageBodyWriter<T> that can be returned from a resource method
Boolean, Character and Number (text/plain) - corresponding primitive types supported via boxing/unboxing conversion

Aside from the basic providers, Jersey also has some non default providers for handling things like JSON to POJO (and vise versa) conversion and handling Multipart. 除了基本提供程序之外,Jersey还具有一些非默认提供程序,用于处理从JSON到POJO(反之亦然)的转换以及处理Multipart。 You can read more about these in Chapter 9 . 您可以在第9章中详细了解这些内容。 If there is a not a provider to handle our conversion needs, then we can also just write our own reader or writer to handle it. 如果没有提供者来处理我们的转换需求,那么我们也可以只编写自己的读取器或写入器来处理它。

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

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