1

I have the following question for you. Currently I am having a RestController that allows calls like this:

@RequestMapping(value = "/notifications", method = RequestMethod.POST)
public void postNotification(@RequestBody final Notification notification) {
       //DoStuffWithTheNotification
}

And here is the notification class:

@JsonPropertyOrder({"body", "sound", "other})
public class Notification {
        @JsonProperty
        String body;
        @JsonProperty
        String sound;
        @JsonProperty
        String other;

        // Bunch of getters,setters and what not
}

Now what I want to do is the following : I want to make a POST-request to this URL, but instead of providing a json-file in the body (as my code would suggest), I would like to call it with an XML-file in the body. Is there an easy way to do this, without making an entirely new Notification object?

Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29
g0belijn
  • 63
  • 4
  • Jackson does have ability to use other formats besides JSON, but in your case you need to have Spring MVC support as well (since Spring MVC deeply integrates Jackson's basic classes). I have never attempted something like this myself, but [this](https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring) link highlights some of Spring's capabilities – geoand Jun 23 '15 at 12:13

2 Answers2

0

take a look at JAXB- an architecture for xml binding. Here is good article: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/

Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
0

If you are using SpringMVC then you can use content negotiator to do this.Below is an small configuration

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  /**
    *  Total customization - see below for explanation.
    */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
  }
}

XMl configuration

<bean id="contentNegotiationManager"
             class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="parameterName" value="mediaType" />
    <property name="ignoreAcceptHeader" value="true"/>
    <property name="useJaf" value="false"/>
    <property name="defaultContentType" value="application/json" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
       </map>
    </property>
</bean>

For futher reference refer here or simply search Spring mvc content negotiation

Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29
  • I am indeed using Spring WebMVC. Your solution looks promising, I'll try to implement it and give you feedback on how it went. – g0belijn Jun 23 '15 at 14:31