Я разрабатываю RESTful API для моего приложения. Все геттеры (которые используют 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.
Я видел, что маршал () вообще не вызывается.
Что не так?
Вот что у меня есть. (Я поставил здесь только соответствующий код.)
Пружинная конфигурация
<?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 + ")");
}
}