Spring Rest api,根据参数区分响应格式

时间:2015-06-17 14:48:16

标签: java spring spring-mvc spring-restcontroller

下面是我的RestController类

@RestController
public class EmployeeRestController {

    @RequestMapping(value = "/employees", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public EmployeeList getEmployeesJson() {
        System.out.println("EMPLOYEES");

        return this.getEmployee();

    }
}

这是我的spring-dispatcher-servlet.xml

  <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.sharique.controller"/>
    <context:component-scan base-package="com.sharique.restcontroller"/>
    <mvc:default-servlet-handler/>

    <!-- To switch on  content negotiation strategies -->
        <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>
    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="order" value="1" />
        <property name="messageConverters">
            <list>
                <!-- Message converters -->

                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                <bean  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </list>
        </property>
    </bean>
        <bean id="viewresolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix">
                <value>/WEB-INF/jsp/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

    </beans>

无论何时击中网址,它都会以xml格式给出响应,但是当基于扩展区分它时,即.xml / .json工作正常。 但我想根据参数来区分响应,请帮助

0 个答案:

没有答案