org.springframework.oxm.Marshaller类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(344)

本文整理了Java中org.springframework.oxm.Marshaller类的一些代码示例,展示了Marshaller类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Marshaller类的具体详情如下:
包路径:org.springframework.oxm.Marshaller
类名称:Marshaller

Marshaller介绍

[英]Defines the contract for Object XML Mapping Marshallers. Implementations of this interface can serialize a given Object to an XML Stream.

Although the marshal method accepts a java.lang.Object as its first parameter, most Marshaller implementations cannot handle arbitrary Objects. Instead, a object class must be registered with the marshaller, or have a common base class.
[中]定义对象XML映射封送器的约定。该接口的实现可以将给定对象序列化为XML流。
尽管封送处理方法接受java。对象作为其第一个参数,大多数Marshaller实现无法处理任意对象。相反,对象类必须在封送处理程序中注册,或者具有公共基类。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
  Assert.notNull(this.marshaller, "Property 'marshaller' is required");
  this.marshaller.marshal(o, result);
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * Write the value objects and flush them to the file.
 * 
 * @param items the value object
 *
 * @throws IOException thrown if general error occurs.
 * @throws XmlMappingException thrown if error occurs during XML Mapping.
 */
@Override
public void write(List<? extends T> items) throws XmlMappingException, IOException {
  if(!this.initialized) {
    throw new WriterNotOpenException("Writer must be open before it can be written to");
  }
  currentRecordCount += items.size();
  for (Object object : items) {
    Assert.state(marshaller.supports(object.getClass()),
        "Marshaller must support the class of the marshalled object");
    Result result = createStaxResult();
    marshaller.marshal(object, result);
  }
  try {
    eventWriter.flush();
    if (forceSync) {
      channel.force(false);
    }            
  }
  catch (XMLStreamException | IOException e) {
    throw new WriteFailedException("Failed to flush the events", e);
  } 
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
  return (supportsMimeType(headers) && this.marshaller != null &&
      this.marshaller.supports(payload.getClass()));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
  return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Marshal the given object to a {@link TextMessage}.
 * @param object the object to be marshalled
 * @param session current JMS session
 * @param marshaller the marshaller to use
 * @return the resulting message
 * @throws JMSException if thrown by JMS methods
 * @throws IOException in case of I/O errors
 * @throws XmlMappingException in case of OXM mapping errors
 * @see Session#createTextMessage
 * @see Marshaller#marshal(Object, Result)
 */
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller)
    throws JMSException, IOException, XmlMappingException {
  StringWriter writer = new StringWriter();
  Result result = new StreamResult(writer);
  marshaller.marshal(object, result);
  return session.createTextMessage(writer.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void renderModelKeyWithJaxbElement() throws Exception {
  String toBeMarshalled = "value";
  String modelKey = "key";
  view.setModelKey(modelKey);
  Map<String, Object> model = new HashMap<>();
  model.put(modelKey, new JAXBElement<>(new QName("model"), String.class, toBeMarshalled));
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(String.class)).willReturn(true);
  marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
  view.render(model, request, response);
  assertEquals("Invalid content type", "application/xml", response.getContentType());
  assertEquals("Invalid content length", 0, response.getContentLength());
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check whether the given value from the current view's model is eligible
 * for marshalling through the configured {@link Marshaller}.
 * <p>The default implementation calls {@link Marshaller#supports(Class)},
 * unwrapping a given {@link JAXBElement} first if applicable.
 * @param modelKey the value's key in the model (never {@code null})
 * @param value the value to check (never {@code null})
 * @return whether the given value is to be considered as eligible
 * @see Marshaller#supports(Class)
 */
protected boolean isEligibleForMarshalling(String modelKey, Object value) {
  Assert.state(this.marshaller != null, "No Marshaller set");
  Class<?> classToCheck = value.getClass();
  if (value instanceof JAXBElement) {
    classToCheck = ((JAXBElement<?>) value).getDeclaredType();
  }
  return this.marshaller.supports(classToCheck);
}

代码示例来源:origin: org.springframework/spring-web

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
  Assert.notNull(this.marshaller, "Property 'marshaller' is required");
  this.marshaller.marshal(o, result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void renderNoModelKeyAndBindingResultFirst() throws Exception {
  Object toBeMarshalled = new Object();
  String modelKey = "key";
  Map<String, Object> model = new LinkedHashMap<>();
  model.put(BindingResult.MODEL_KEY_PREFIX + modelKey, new BeanPropertyBindingResult(toBeMarshalled, modelKey));
  model.put(modelKey, toBeMarshalled);
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(BeanPropertyBindingResult.class)).willReturn(true);
  given(marshallerMock.supports(Object.class)).willReturn(true);
  view.render(model, request, response);
  assertEquals("Invalid content type", "application/xml", response.getContentType());
  assertEquals("Invalid content length", 0, response.getContentLength());
  verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
}

代码示例来源:origin: org.springframework/spring-web

@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
  return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
  Object toBeMarshalled = locateToBeMarshalled(model);
  if (toBeMarshalled == null) {
    throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model);
  }
  Assert.state(this.marshaller != null, "No Marshaller set");
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
  this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));
  setResponseContentType(request, response);
  response.setContentLength(baos.size());
  baos.writeTo(response.getOutputStream());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void renderNoModelKey() throws Exception {
  Object toBeMarshalled = new Object();
  String modelKey = "key";
  Map<String, Object> model = new HashMap<>();
  model.put(modelKey, toBeMarshalled);
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(Object.class)).willReturn(true);
  view.render(model, request, response);
  assertEquals("Invalid content type", "application/xml", response.getContentType());
  assertEquals("Invalid content length", 0, response.getContentLength());
  verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
}

代码示例来源:origin: org.springframework/spring-webmvc

/**
 * Check whether the given value from the current view's model is eligible
 * for marshalling through the configured {@link Marshaller}.
 * <p>The default implementation calls {@link Marshaller#supports(Class)},
 * unwrapping a given {@link JAXBElement} first if applicable.
 * @param modelKey the value's key in the model (never {@code null})
 * @param value the value to check (never {@code null})
 * @return whether the given value is to be considered as eligible
 * @see Marshaller#supports(Class)
 */
protected boolean isEligibleForMarshalling(String modelKey, Object value) {
  Assert.state(this.marshaller != null, "No Marshaller set");
  Class<?> classToCheck = value.getClass();
  if (value instanceof JAXBElement) {
    classToCheck = ((JAXBElement<?>) value).getDeclaredType();
  }
  return this.marshaller.supports(classToCheck);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers,
    @Nullable Object conversionHint) {
  Assert.notNull(this.marshaller, "Property 'marshaller' is required");
  try {
    if (byte[].class == getSerializedPayloadClass()) {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      Result result = new StreamResult(out);
      this.marshaller.marshal(payload, result);
      payload = out.toByteArray();
    }
    else {
      Writer writer = new StringWriter();
      Result result = new StreamResult(writer);
      this.marshaller.marshal(payload, result);
      payload = writer.toString();
    }
  }
  catch (Throwable ex) {
    throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
  }
  return payload;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void renderModelKey() throws Exception {
  Object toBeMarshalled = new Object();
  String modelKey = "key";
  view.setModelKey(modelKey);
  Map<String, Object> model = new HashMap<>();
  model.put(modelKey, toBeMarshalled);
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(Object.class)).willReturn(true);
  marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
  view.render(model, request, response);
  assertEquals("Invalid content type", "application/xml", response.getContentType());
  assertEquals("Invalid content length", 0, response.getContentLength());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testRenderUnsupportedModel() throws Exception {
  Object toBeMarshalled = new Object();
  String modelKey = "key";
  Map<String, Object> model = new HashMap<>();
  model.put(modelKey, toBeMarshalled);
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(Object.class)).willReturn(false);
  try {
    view.render(model, request, response);
    fail("IllegalStateException expected");
  }
  catch (IllegalStateException ex) {
    // expected
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Marshal the given object to a {@link BytesMessage}.
 * @param object the object to be marshalled
 * @param session current JMS session
 * @param marshaller the marshaller to use
 * @return the resulting message
 * @throws JMSException if thrown by JMS methods
 * @throws IOException in case of I/O errors
 * @throws XmlMappingException in case of OXM mapping errors
 * @see Session#createBytesMessage
 * @see Marshaller#marshal(Object, Result)
 */
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller)
    throws JMSException, IOException, XmlMappingException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
  StreamResult streamResult = new StreamResult(bos);
  marshaller.marshal(object, streamResult);
  BytesMessage message = session.createBytesMessage();
  message.writeBytes(bos.toByteArray());
  return message;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void renderModelKeyUnsupported() throws Exception {
  Object toBeMarshalled = new Object();
  String modelKey = "key";
  view.setModelKey(modelKey);
  Map<String, Object> model = new HashMap<>();
  model.put(modelKey, toBeMarshalled);
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  given(marshallerMock.supports(Object.class)).willReturn(false);
  try {
    view.render(model, request, response);
    fail("IllegalStateException expected");
  }
  catch (IllegalStateException ex) {
    // expected
  }
}

代码示例来源:origin: org.springframework/spring-webmvc

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
  Object toBeMarshalled = locateToBeMarshalled(model);
  if (toBeMarshalled == null) {
    throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model);
  }
  Assert.state(this.marshaller != null, "No Marshaller set");
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
  this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));
  setResponseContentType(request, response);
  response.setContentLength(baos.size());
  baos.writeTo(response.getOutputStream());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void canWrite() {
  Marshaller marshaller = mock(Marshaller.class);
  given(marshaller.supports(Integer.class)).willReturn(false);
  given(marshaller.supports(String.class)).willReturn(true);
  MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
  converter.setMarshaller(marshaller);
  assertFalse(converter.canWrite(Boolean.class, MediaType.TEXT_PLAIN));
  assertFalse(converter.canWrite(Integer.class, MediaType.TEXT_XML));
  assertTrue(converter.canWrite(String.class, MediaType.TEXT_XML));
}

相关文章

微信公众号

最新文章

更多