Package com.fasterxml.jackson.annotation

Examples of com.fasterxml.jackson.annotation.JsonView


    {
        JacksonJsonProvider prov = new JacksonJsonProvider();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Bean bean = new Bean();
        Method m = getClass().getDeclaredMethod("bogus");
        JsonView view = m.getAnnotation(JsonView.class);
        assertNotNull(view); // just a sanity check
        prov.writeTo(bean, bean.getClass(), bean.getClass(), new Annotation[] { view },
                MediaType.APPLICATION_JSON_TYPE, null, out);
        assertEquals("{\"value1\":1}", out.toString("UTF-8"));
    }
View Full Code Here


        JacksonJsonProvider prov = new JacksonJsonProvider();
        prov.setDefaultWriteView(MyView2.class);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Bean bean = new Bean();
        Method m = getClass().getDeclaredMethod("bogus");
        JsonView view = m.getAnnotation(JsonView.class);
        assertNotNull(view);
        prov.writeTo(bean, bean.getClass(), bean.getClass(), new Annotation[0],
                MediaType.APPLICATION_JSON_TYPE, null, out);
        assertEquals("{\"value2\":2}", out.toString("UTF-8"));
    }
View Full Code Here

  @Override
  protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
      MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

    JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
    Class<?>[] classes = annotation.value();
    if (classes.length != 1) {
      throw new IllegalArgumentException(
          "@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
    }
    bodyContainer.setSerializationView(classes[0]);
View Full Code Here

    {
        JacksonXMLProvider prov = new JacksonXMLProvider();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Bean bean = new Bean();
        Method m = getClass().getDeclaredMethod("bogus");
        JsonView view = m.getAnnotation(JsonView.class);
        assertNotNull(view); // just a sanity check
        prov.writeTo(bean, bean.getClass(), bean.getClass(), new Annotation[] { view },
                MediaType.APPLICATION_JSON_TYPE, null, out);
        assertEquals("<Bean><value1>1</value1></Bean>", out.toString("UTF-8"));
    }
View Full Code Here

    try {
      JsonFactory jsonFactory = mapper.getFactory();
      JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);

      JsonView view = returnType.getMethodAnnotation(JsonView.class);
      Class<?>[] viewClass = view.value();
      notEmpty(viewClass, "The view class is missing: " + returnType);
      // prepare a writer
      ObjectWriter objectWriter = mapper.writerWithView(viewClass[0]);
      // write output
      objectWriter.writeValue(jsonGenerator, returnValue);
View Full Code Here

TOP

Related Classes of com.fasterxml.jackson.annotation.JsonView

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.