java - 使用自定义编码器的 REST+Spring+POST

标签 java spring rest

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

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

这是我的 Controller

@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() 根本没有被调用。

出了什么问题?

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

Spring 配置

<?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 + ")");
 }
}

最佳答案

@RequestBody/@ResponseBodyHttpMessageConverter 的层次结构支持,这与 ViewResolver 完全不同>s.

在这种情况下,您需要配置 MarshallingHttpMessageConverter使用适当的编码器/解码器和内容类型(或者创建您自己的 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);
        }
    }
    ...
}

关于java - 使用自定义编码器的 REST+Spring+POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4389372/

相关文章:

java - 使用 datastax 从列表类型获取数据时出错

java - Android Firestore 复合查询

java - android studio中的src文件夹没有

java - MongoTemplate - 带有 JS 函数的地方

java - 更新到SDN 3.4.1后出现异常 "The Neo Server running is of unknown type."

java - 当用户在 spring boot 应用程序中输入无效 URL 时如何显示自定义 404 页面?

javascript - 在 jsp 文件中使用 php 访问完整日历插件

java - 使用 spring boot 进行 rest webservices 异常处理

url - 用于资源收集的 REST url,它按不等于给定值的属性过滤资源

web-services - 使用 typescript 使用 Web 服务