@RestResource(path="/photos", messages="nls/PhotosResource", title="Photo REST service", description="Sample resource that allows images to be uploaded and retrieved.", swagger=) @HtmlDocConfig(navlinks={"up: request:/..","options: servlet:/?method=OPTIONS","$W{UploadPhotoMenuItem}","source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"}, aside={"<div style=\'max-width:400px;min-width:200px\' class=\'text\'>","\t<p>Shows an example of using custom serializers and parsers to create REST interfaces over binary resources.</p>","\t<p>In this case, our resources are marshalled jpeg and png binary streams and are stored in an in-memory \'database\' (also known as a <c>TreeMap</c>).</p>","</div>"}, widgets=UploadPhotoMenuItem.class, stylesheet="servlet:/htdocs/themes/dark.css") @HtmlConfig(uriAnchorText="SERVLET_RELATIVE") public class PhotosResource extends BasicRestServlet
Modifier and Type | Class and Description |
---|---|
static class |
PhotosResource.ImageParser
Parser for converting byte streams to images
|
static class |
PhotosResource.ImageSerializer
Serializer for converting images to byte streams
|
static class |
PhotosResource.Photo
Our bean class for storing photos
|
Constructor and Description |
---|
PhotosResource() |
Modifier and Type | Method and Description |
---|---|
String |
addPhoto(String id,
BufferedImage image)
Shows how to use a custom parser to parse a stream into a BufferedImage object.
|
String |
deletePhoto(String id)
Removes a photo from the database.
|
Collection<PhotosResource.Photo> |
getAllPhotos()
Show the list of all currently loaded photos
|
BufferedImage |
getPhoto(String id)
Shows how to use a custom serializer to serialize a BufferedImage object to a stream.
|
void |
init() |
PhotosResource.Photo |
setPhoto(BufferedImage image)
Shows how to use a custom parser to parse a stream into a BufferedImage object.
|
SeeOtherRoot |
uploadFile(RestRequest req)
Upload a photo from a multipart form post.
|
getOptions
destroy, getContext, getPath, getProperties, getRequest, getResponse, getServletConfig, init, log, log, log, log, logObjects, service, setContext, setRestResourceResolver
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
getInitParameter, getInitParameterNames, getServletContext, getServletInfo, getServletName
public PhotosResource()
public void init()
init
in class GenericServlet
@RestMethod(name="GET", path="/", summary="Show the list of all currently loaded photos") public Collection<PhotosResource.Photo> getAllPhotos() throws Exception
Exception
- Error occurred.@RestMethod(name="GET", path="/{id}", serializers=PhotosResource.ImageSerializer.class, summary="Get a photo by ID", description="Shows how to use a custom serializer to serialize a BufferedImage object to a stream.") @Response(schema=@Schema(type="file")) public BufferedImage getPhoto(@Path(value="id") String id) throws NotFound
id
- The photo ID.NotFound
- Image was not found.@RestMethod(name="PUT", path="/{id}", parsers=PhotosResource.ImageParser.class, summary="Add or overwrite a photo", description="Shows how to use a custom parser to parse a stream into a BufferedImage object.") public String addPhoto(@Path(value="id") String id, @Body(description="Binary contents of image.",schema=@Schema(type="file")) BufferedImage image) throws Exception
id
- The photo ID.image
- Binary contents of image.Exception
- Error occurred.@RestMethod(name="POST", path="/", parsers=PhotosResource.ImageParser.class, summary="Add a photo", description="Shows how to use a custom parser to parse a stream into a BufferedImage object.") public PhotosResource.Photo setPhoto(@Body(description="Binary contents of image.",schema=@Schema(type="file")) BufferedImage image) throws Exception
image
- Binary contents of image.Exception
- Error occurred.@RestMethod(name="POST", path="/upload", matchers=MultipartFormDataMatcher.class, summary="Upload a photo from a multipart form post", description="Shows how to parse a multipart form post containing a binary field.", swagger=) public SeeOtherRoot uploadFile(RestRequest req) throws Exception
req
- HTTP request.Exception
- Error occurred.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.