Results 1 to 2 of 2

Thread: XML in, JSON out

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Default XML in, JSON out

    I am working with Web MVC/REST.

    The problem is that I want to have my servlet only receive xml but only return json. I can't figure out how to do this in my Web MVC code. At this time my servlet only returns xml.

    The following snippets are not cut and paste, so please ignore typos.

    Code Snippet

    @RequestMapping(value = "/search", method=RequestMethod.POST, headers={"Accept=application/xml, application/json"}
    public @ResponseBody OutputDto<ResultVo> searchFoo {
    @RequestBody Search inputBean) {
    OutputDto foo = doSomething();
    return foo;
    }

    node-servlet.xml Snippet

    <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
    <bean id ="jsonMessageConverter" class="...MappingJacksonHttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper"/>
    <property name="supportedMediaTypes" value="application/json"/>
    </bean>

    <bean class="...AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <list>
    <ref bean="jsonMessageConverter"/>
    <ref bean="xmlMessageConverter"/>
    </list>
    </property>
    </bean>

  2. #2
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, have you tried to use MappingJacksonJsonView?

    Code:
    @RequestMapping("/jsontournament")
    public String getJSON(Model model) {
      List<TournamentContent> tournamentList = new ArrayList<TournamentContent>();
        tournamentList.add(TournamentContent.generateContent("FIFA",
          new Date(),"World Cup","www.fifa.com/worldcup/"));
        tournamentList.add(TournamentContent.generateContent("FIFA",
          new Date(),"U-20 World Cup","www.fifa.com/u20worldcup/"));
        tournamentList.add(TournamentContent.generateContent("FIFA",
          new Date(),"U-17 World Cup","www.fifa.com/u17worldcup/"));
        tournamentList.add(TournamentContent.generateContent("FIFA", 
          new Date(),"Confederations Cup","www.fifa.com/confederationscup/"));
        model.addAttribute("feedContent",tournamentList);
        return "jsontournamenttemplate";
    }
    Code:
    <bean id="jsontournamenttemplate" 
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •