How to reuse Jersey's JSON/JAXB for serialization?

前端 未结 7 1350
失恋的感觉
失恋的感觉 2020-11-29 23:48

I have a JAX-RS REST service implemented using Jersey. One of the cool features of JAX-RS/Jersey is how easily a POJO can be turned into a REST service, simply by sprinkling

7条回答
  •  半阙折子戏
    2020-11-30 00:40

    With a little Jersey specific bootstrapping, you can use it to create the necessary JSON objects for you. You need to include the following dependencies (you can use bundle, but it will cause problems if you are using Weld for testing):

        
            com.sun.jersey
            jersey-json
            1.12
        
        
            com.sun.jersey
            jersey-client
            1.12
        
    

    From there you can create a JAXB annotated class. The following is an example:

    @XmlRootElement
    public class TextMessage {
    private String text;
        public String getText() { return text; }
        public void setText(String s) { this.text = text; }
    }
    

    Then you can create the following unit test:

        TextMessage textMessage = new TextMessage();
        textMessage.setText("hello");
        textMessage.setUuid(UUID.randomUUID());
    
        // Jersey specific start
        final Providers ps = new Client().getProviders();
        // Jersey specific end
        final MultivaluedMap responseHeaders = new MultivaluedMap() {
    
            @Override
            public void add(final String key, final Object value) {
            }
    
            @Override
            public void clear() {
            }
    
            @Override
            public boolean containsKey(final Object key) {
                return false;
            }
    
            @Override
            public boolean containsValue(final Object value) {
                return false;
            }
    
            @Override
            public Set>> entrySet() {
                return null;
            }
    
            @Override
            public List get(final Object key) {
                return null;
            }
    
            @Override
            public Object getFirst(final String key) {
                return null;
            }
    
            @Override
            public boolean isEmpty() {
                return false;
            }
    
            @Override
            public Set keySet() {
                return null;
            }
    
            @Override
            public List put(final String key, final List value) {
                return null;
            }
    
            @Override
            public void putAll(
                    final Map> m) {
            }
    
            @Override
            public void putSingle(final String key, final Object value) {
            }
    
            @Override
            public List remove(final Object key) {
                return null;
            }
    
            @Override
            public int size() {
                return 0;
            }
    
            @Override
            public Collection> values() {
                return null;
            }
        };
    
        final MessageBodyWriter messageBodyWriter = ps
                .getMessageBodyWriter(TextMessage.class, TextMessage.class,
                        new Annotation[0], MediaType.APPLICATION_JSON_TYPE);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Assert.assertNotNull(messageBodyWriter);
    
        messageBodyWriter.writeTo(textMessage, TextMessage.class,
                TextMessage.class, new Annotation[0],
                MediaType.APPLICATION_JSON_TYPE, responseHeaders, baos);
        final String jsonString = new String(baos.toByteArray());
        Assert.assertTrue(jsonString.contains("\"text\":\"hello\""));
    
    
    

    The advantage to this approach is it keeps everything within the JEE6 API, no external libraries are explicitly needed except for testing and getting the providers. However, you need to create an implementation of MultivaluedMap since there is nothing provided in the standard and we don't actually use it. It may also be slower than GSON, and a lot more complicated than necessary.

    提交回复
    热议问题