Announcement Announcement Module
Collapse
No announcement yet.
XML in, JSON out Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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"/>

    Comment

    Working...
    X