【问题标题】:REST+Spring+POST with custom marshaller带有自定义编组器的 REST+Spring+POST
【发布时间】:2011-05-22 07:43:25
【问题描述】:

我正在为我的应用程序开发 RESTful API。所有 getter(使用 HTTP GET)都可以正常工作。我无法使保存方法(使用 POST)工作。

我正在使用 HTML 表单和 RESTClient 进行测试。

这是我的控制器

@Controller
public class EntitiesController {
 @RequestMapping(value="/ci/save/", method = RequestMethod.POST)
 public ModelAndView saveConfigurationItem(@RequestBody ConfigurationItem body) {
  System.out.println("saveConfigurationItem: body=" + body);
  return createModelAndView("ci", Collections.emptyList());
 }
}

这个方法应该在客户端发布 ConfigurationItem 时被调用。 我正在使用自定义序列化格式。它不是 XML 或 JSON。它是 VCard 或 VCalendar 格式。对于我的第一次测试,我使用了以下 VCard:

BEGIN:VCARD
N:Pooh;Winnie
FN:Winnie the Pooh
TEL:tel:+441234567
END:VCARD

我将其发布到 URL http://localhost:8080/core.solution-1.0/data/ci/save/。 这是我得到的回复:

415
The server refused this request because the request entity is in a format not
supported by the requested resource for the requested method ()

(*) ConfigurationItem 是一个抽象类。 CardEntry 扩展了它。我都试过了。

我尝试将方法参数更改为String。在这种情况下,该方法被调用但字符串为空。遵循我在网络上看到的一项建议时也会发生同样的情况,我将参数类型更改为 MultiValueMap 并从简单的 HTML 表单发送请求。

我看到根本没有调用 marshal()。

怎么了?

这就是我所拥有的。 (我这里只放了相关代码。)

弹簧配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:oxm="http://www.springframework.org/schema/oxm"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

 <import resource="classes/spring-config-prod.xml"/>
 <context:component-scan base-package="com.mycompany.solution.service" />


 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

 <bean id="ciCardView" class="com.mycompany.solution.service.VFormatView">
  <constructor-arg>
   <bean class="com.mycompany.solution.service.VFormatMarshaller">
    <property name="packagesToScan" value="com.mycompany.solution.entity"/>
   </bean>
  </constructor-arg>
 </bean>
</beans>

编组器

public class VFormatMarshaller implements Marshaller, Unmarshaller {
 @Override
 public void marshal(Object obj, Result result)
   throws IOException/*, XmlMappingException*/ {
  System.out.println("VFormatMarshaller.marshal(" + obj + ")");
  marshalStreamResult(obj, (StreamResult)result);  
 }

 @Override
 public boolean supports(Class<?> paramClass) {
  System.out.println("VFormatMarshaller.supports(" + paramClass + ")");
  boolean supports = new HashSet<String>(Arrays.asList(packagesToScan)).contains(paramClass.getPackage().getName());
  if (supports) {
   return supports;
  }

  return Collection.class.isAssignableFrom(paramClass);
 }

 @Override
 public Object unmarshal(Source source) throws IOException/*, XmlMappingException*/ {
  System.out.println("VFormatMarshaller.unmarshal(" + source + ")");
  return unmarshalStreamSource((StreamSource)source);
 }
//// .............................
}

查看(仅用于覆盖内容类型)

public class VFormatView extends MarshallingView {

 public VFormatView() {
  super();
  setContentType("application/vcard");
  System.out.println("VFormatView()");
 }

 public VFormatView(Marshaller marshaller) {
  super(marshaller);
  setContentType("application/vcard");
  System.out.println("VFormatView(" + marshaller + ")");
 }
}

【问题讨论】:

  • 在控制器中使用类型化参数时,日志中是否出现异常?

标签: java spring rest


【解决方案1】:

@RequestBody/@ResponseBodyHttpMessageConverters 的层次结构支持,这与ViewResolvers 完全不同。

在您的情况下,您需要为 MarshallingHttpMessageConverter 配置适当的 marshaller/unmarshaller 和内容类型(或者如果您不需要依赖 marshaller/unmarshaller 的现有实现,则创建您自己的 HttpMessageConverter),并提供AnnotationMethodHandlerAdapter 的配置实例。

配置自定义HttpMessageConveter 的最少干扰方式是创建BeanPostProcessor,如下所示:

public class Configurer implements BeanPostProcessor {
    public void postProcessAfterInitialization(String name, Object bean) {
        if (bean instanceof AnnotationMethodHandlerAdapter) {
            AnnotationMethodHandlerAdapter a = (AnnotationMethodHandlerAdapter) bean;
            HttpMessageConverter[] convs = a.getMessageConverters();
            ... add new converter ...
            a.setMessageConverters(convs);
        }
    }
    ...
}

【讨论】:

  • 谢谢!有没有好的配置示例供参考?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 2013-11-23
  • 2021-08-12
相关资源
最近更新 更多