Java SpringRESTAPI,基于参数区分响应格式

Java SpringRESTAPI,基于参数区分响应格式,java,spring,spring-mvc,spring-restcontroller,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() { S

下面是我的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>

/WEB-INF/jsp/
.jsp
每当我点击url时,它都会以xml格式给我响应,但当我根据扩展名(即.xml/.json)对其进行区分时,它工作得很好。
但是我想根据参数区分响应,请帮助

您尝试发送了吗?我发誓我之前看到了完全相同的问题(被删除了?)。我评论说,您应该删除
注释方法HandlerAdapter
,因为它很旧,您应该使用
标记来注册转换器。但是spring会自动为xml或json转换注册转换,因此您不需要为此进行任何配置。您是否尝试发送?我发誓我在前面看到了完全相同的问题(删除了?)。我评论说,您应该删除
注释方法HandlerAdapter
,因为它很旧,您应该使用
标记来注册转换器。但是spring会自动为xml或json转换注册转换,因此您不需要为此进行任何配置。您是否尝试发送?我发誓我在前面看到了完全相同的问题(删除了?)。我评论说,您应该删除
注释方法HandlerAdapter
,因为它很旧,您应该使用
标记来注册转换器。但是,spring会自动注册xml或json转换的转换,因此您不需要为此配置任何东西。