开发者

REST+Spring+POST with custom marshaller

I am developing RESTful API for my application. All getters (that use HTTP GET) work fine. I cannot make save method (that uses POST) to work.

I am using HTML form and RESTClient for testing.

Here is my 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());
 }
}

This method is expected to be called when client posts ConfigurationItem. I am using custom serialization format. It is not XML or JSON. It is VCard or VCalendar format. For my first test I used the following VCard:

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

I posted it to URL http://localhost:8080/core.solution-1.0/data/ci/save/. Here is the response I get:

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 is an abstract class. CardEntry extends it. I tried both.

I tried to change the method parameter to String. In this case the method is called but the string is empty. The same happens when following one of recommendations I saw in web I changed the parameter type to MultiValueMap and sent request from simple HTML form.

I saw that marshal() is not called at all.

What's wrong?

Here is what I have. (I put here relevant code only.)

Spring configuration

<?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>

Marshaller

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;
 开发者_JAVA技巧 }

  return Collection.class.isAssignableFrom(paramClass);
 }

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

View (this is written only to override the content type)

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 are supported by an hierarchy of HttpMessageConverters, that is completely different from ViewResolvers.

In you case you need to configure a MarshallingHttpMessageConverter with appropriate marshaller/unmarshaller and content type (or create your own HttpMessageConverter if you don't need to depend on the existing implementation of marshaller/unmarshaller), and supply a configured instance to AnnotationMethodHandlerAdapter.

The least intrusive way to configure a custom HttpMessageConveter is to create a BeanPostProcessor as follows:

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);
        }
    }
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜