Reputation: 3239
I have configured an application that returns json and xml views depending on whats appended on the end of the url.
as per:
http://pfelitti87.blogspot.co.uk/2012/07/rest-services-with-spring-3-xml-json.html
I want to re-configure it but only allow xml to be returned.
Not sure how to do that, everything i have done still returns json, when nothing is appended to the end
@Bean(name = "viewResolver")
public ContentNegotiatingViewResolver viewResolver() {
ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
contentNegotiatingViewResolver.setOrder(1);
contentNegotiatingViewResolver.setFavorPathExtension(true);
contentNegotiatingViewResolver.setFavorParameter(true);
contentNegotiatingViewResolver.setIgnoreAcceptHeader(false);
Map<String, String> mediaTypes = new HashMap<String, String>();
mediaTypes.put("xml", "application/xml");
contentNegotiatingViewResolver.setMediaTypes(mediaTypes);
List<View> defaultViews = new ArrayList<View>();
defaultViews.add(xmlView());
contentNegotiatingViewResolver.setDefaultViews(defaultViews);
return contentNegotiatingViewResolver;
}
so the output goes like this :
.../state.json error no jsp page json
.../state.xml works fine
.../state error no jsp page
What i want is the the following two urls to return xml
.../state
.../state.xml
The above does not work..
and this is the controller mapping
@RequestMapping(value= "/{id}/{ref}/state", method = RequestMethod.GET)
public state getState(...) {...}
Upvotes: 1
Views: 1404
Reputation: 2137
here's a solution configured through xml; - the controller
@Controller
@RequestMapping("/simpleEntity")
public class SimpleRestController {
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody SimpleEntity get() {
return new SimpleEntity();
}
}
the xml
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstream"/>
<property name="unmarshaller" ref="xstream"/>
<property name="supportedMediaTypes">
<list>
<value>#{T(org.springframework.http.MediaType).TEXT_XML_VALUE}</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="de.incompleteco.spring.web"/>
<bean id="xstream" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
here's a Configuration version
@Configuration
@EnableWebMvc
@ComponentScan("de.incompleteco.spring.web")
public class SimpleConfiguration extends WebMvcConfigurerAdapter {
private XStreamMarshaller marshaller;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(getHttpMessageConverter());
super.configureMessageConverters(converters);
}
public HttpMessageConverter<Object> getHttpMessageConverter() {
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setMarshaller(marshaller());
converter.setUnmarshaller(marshaller());
return converter;
}
@Bean
public AbstractMarshaller marshaller() {
if (marshaller == null) {
return new XStreamMarshaller();
} else {
return marshaller;
}
}
}
Upvotes: 2
Reputation: 388446
Try the following
Remove jackson from classpath
Remove the entry <entry key="json" value="application/json" />
from mediaTypes
of ContentNegotiatingViewResolver
.
Remove the bean <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
from defaultViews
of ContentNegotiatingViewResolver
.
Upvotes: 0