Announcement Announcement Module
Collapse
No announcement yet.
Common controller method to service JSON and Servlet Model based requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Common controller method to service JSON and Servlet Model based requests

    Hi,

    I am using Spring 3.2.2.RELEASE version. I am trying to create a controller that serves empty form using regular JSP ( Model - View ) as well as provide JSON output of empty object for REST based client to use.

    I have currently configured the following controller

    Code:
    @RequestMapping(value = "/new/{pajType}", method = RequestMethod.GET)
    public @ResponseBody ModelMap initPAJForm(@PathVariable("pajType") String productType, HttpSession session) {
            logger.info(productType);
            JibForm initJobForm = new JibForm(productType);
            return new ModelMap("newJib", initJibForm);
    }
    My appContext.xml is as follows:

    HTML Code:
     <mvc:annotation-driven  content-negotiation-manager="contentNegotiationManager" />
        <!-- this is where the controller should go -->
        <!-- in case if we are using xml based sprin setup -->
        <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
        <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
            <property name="favorPathExtension" value="true" />
            <property name="ignoreAcceptHeader" value="false" />
            <property name="mediaTypes" >
                <value>
                    json=application/json
                    xml=application/xml
                </value>
            </property>
        </bean>
    My Spring test Class is as below [ It works ]

    Code:
    @Test
    public void initPostAJib() throws Exception {
        this.mockMvc.perform(get("/postJib/new/online.json").accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk()).andExpect(jsonPath("$.newJib.jibType").value("on"));
       this.mockMvc.perform(get("/postJib/new/online.json").accept(MediaType.APPLICATION_JSON))
                        .andExpect(status().isOk()).andExpect(jsonPath("$.newJib.jibLock").value("true"));
    }
    But now I want to the above controller method to work for JSP too where if url is only /postJib/new/online then it should invoke JSP with the relevant model JibForm

    I am kind of lost as to how I can achive this or if I can at all achieve this.

    Any help is highly appreciated.

    Thanks
Working...
X