s.butenin
(Сергей Бутенин)
September 5, 2019, 11:03am
#1
Hello!
I have attached ‘com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.9’ to project and received wrong behaviour with REST.
Now if I send request to REST without Accept header ‘application/json’ response is with XML.
For example:
/app/rest/v2/oauth/token?grant_type=password&username=service&password=service
Produce such XML response:
<InvalidGrantException>
<error>invalid_grant</error>
<error_description>Bad credentials</error_description>
</InvalidGrantException>
When I add header Accept. There is OK, response is JSON.
How to change default content type for REST, if not specified Accept header?
In SO recommends
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}
}
Is this can help me? Or may be we can change default content by application properties?
Attaching sample project with such problem.
testxml.zip (91.1 KB)
gorbunkov
(Maxim Gorbunkov)
September 11, 2019, 12:12pm
#3
Hi,
right now the option I see is to configure the content negotiation manager in the rest-dispatcher-spring.xml
file. We’ve created the issue - the JSON content type will be default. Until the issue is solved you can do the following:
In the web module create the first-rest-dispatcher-spring.xml
file that registers the content negotiation manager:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc">
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/json"/>
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
</beans>
Register the file in the web-app.properties file:
cuba.restSpringContextConfig = com/company/testxml/first-rest-dispatcher-spring.xml com/haulmont/cuba/rest-dispatcher-spring.xml
Note that there is no +
sign there and your file is registered before the com/haulmont/cuba/rest-dispatcher-spring.xml
of CUBA.
Here is the zip archive with your sample project where these modifications are done: testxml.zip (97.1 KB)
s.butenin
(Сергей Бутенин)
September 11, 2019, 4:47pm
#5
@gorbunkov Max, thank you for your solution. I’ll use it in the future.
Another solutuion is add header by reverse proxy. My app is under reverse proxy Nginx and I added this header for workaround.
Thank you!
s.butenin
(Сергей Бутенин)
September 12, 2019, 6:53am
#6
Max, thank you for your solution. I’ use it in the future. Now I solved problem by adding this header in Nginx reverse proxy.