我正在为我的应用程序开发 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
/@ResponseBody
由 HttpMessageConverter
的层次结构支持,这与 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/