1. Domino REST
  2. Rest clients
  3. Produces and Consumes

Produces and Consumes

Domino-rest use JaxRs @Produces and @Consumes and By default all service methods are mapped to produce or consume MediaType.APPLICATION_JSON which can be omitted, since the default serialization/deserialization in domino-rest is JSON, domino-rest supports both JSON and plain text by default but to use any other format writing custom (de)serializers is required and we utilize domino-rest RequestWriter/ResponseReader for that.

  • Request writer

    If our API expect the request body to be in a format that is not JSON or plain text we can provide a custom serializer or writer that implements the generic interface RequestWriter<T>

    For example if we need the update method to send the movie in the body in xml format instead of JSON, we introduce a writer class :

    			public class XmlMovieWriter implements RequestWriter<Movie>{
            @Override
            public String write(Movie request) {
                String movieXml = //convert the movie to xml
                return movieXml;
            }
        }
    		

    Then in the service definition, we change the @Consumes and specify the writer using the @Writer annotation :

    			@RequestFactory
    public interface MoviesService {
    
        @Path("library/movies/:movieName")
        @GET
        Movie getMovieByName(@PathParam("movieName") String movieName);
    
        @Path("library/movies")
        @GET
        List<Movie> listMovies();
    
        @Path("movies/:name")
        @PUT
        @Consumes(MediaType.APPLICATION_XML)
        @Writer(MovieXmlWriter.class)
        void updateMovie(@BeanParam @RequestBody Movie movie);
    }
    		

    In case we cant add the @Writer annotation on the JAX-RS resource method we can define the writer externally using the CustomMapper class like the following :

    			CustomMapper.matcher(meta -> MoviesService.class.equals(meta.getServiceClass())
                    && MediaType.APPLICATION_ATOM_XML.equals(meta.getConsume())
                    && Movie.class.equals(meta.getRequestClass()))
                    .writer(() -> new XmlMovieWriter());
    		
  • Response reader

    If our API should return the response body in a format that is not JSON or plain text we can provide a custom deserializer or reader that implements the generic interface ResponseReader<T>

    For example if we need the update method to read the movie from the response body in xml format instead of JSON, we introduce a reader class :

    			    public class XmlMovieReader implements RequestReader<Movie>{
            @Override
            public Movie read(String request) {
                Movie movieFromXml = //convert the xml to Movie
                return movieFromXml;
            }
        }
    		

    Then in the service definition, we change the @Produces and specify the reader using the @Reader annotation :

    			@RequestFactory
    public interface MoviesService {
    
        @Path("library/movies/:movieName")
        @GET
        @Produce(MediaType.APPLICATION_XML)
        @Reader(XmlMovieReader.class)
        Movie getMovieByName(@PathParam("movieName") String movieName);
    
        @Path("library/movies")
        @GET
        List<Movie> listMovies();
    
        @Path("movies/:name")
        @PUT
        @Consumes(MediaType.APPLICATION_XML)
        @Writer(MovieXmlWriter.class)
        void updateMovie(@BeanParam @RequestBody Movie movie);
    }
    		

    In case we cant add the @Reader annotation on the JAX-RS resource method we can define the reader externally using the CustomMapper class like the following :

    			CustomMapper.matcher(meta -> MoviesService.class.equals(meta.getServiceClass())
                    && MediaType.APPLICATION_ATOM_XML.equals(meta.getProduce())
                    && Movie.class.equals(meta.getResponseClass()))
                    .reader(() -> new XmlMovieReader());
    		

We are a group of passionate people who love what we do

Donate & Support Us