简体   繁体   中英

RESTful Webservices return types

I am new to webservices in Java. and I am writing web services using JAX-RS implementation Jersey .

I'd like to know:

  • Which return types are possible for web service methods?

  • As far as I know you can't use primitive types as return type for web service method.. why so?

Read Chapter 8: JAX-RS Entity Providers in the Jersey docs. It explains how (de)serialization is done in Jersey. It explains the use of MessageBodyReader s and MessageBodyWriter s to transform the data to and from Java object to serialized streams.

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. You can read more about these in Chapter 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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