尝试使用Ajax请求从弹簧控制器返回视图时获取415不支持的介质类型错误

时间:2013-09-06 09:26:14

标签: java ajax spring jsp

我有两个控制器方法

@RequestMapping(value = "/link", method = RequestMethod.GET) 
public ModelAndView link(HttpServletRequest httpRequest, @RequestParam(value="name", required=false) String name, @RequestParam(value="id", required=false) String id, @RequestParam(value="type") String type) {

    ModelAndView mav=new ModelAndView("ViewPage");

    SearchRequest request = new SearchRequest();
    request.setName(name);
    request.setId(id);
    request.setType(type);

    mav.addObject("Request", request);

}

@RequestMapping(value="/find", headers="Accept=/", method=RequestMethod.POST) 
public @ResponseBody List find(HttpServletRequest httpRequest, @RequestBody SearchRequest searchRequest) {

}

从第一个控制器方法链接,控件将传递给ViewPage.jsp,我们将ModelView对象传递给ViewPage.jsp。并且控制应该再次找到方法。

$(document).ready(function(){


    var myJSON  = {name:"test", id:"test", type:"test"}; 
    myJSON = JSON.stringify(myJSON);


    $.ajax({
            type: "POST",
            url: "../find",         
            dataType:'JSON',
            data: myJSON,
            cache: false,
            success: function(data){

            if(data!=""){

            }
            )}
    }

我收到以下错误

“NetworkError:415不支持的媒体类型 - localhost:8080 / myreport / find”

2 个答案:

答案 0 :(得分:0)

在Spring XML配置中,您需要指定支持的媒体类型,如此...

<beans:property name="mediaTypes">
     <beans:map>
         <beans:entry key="html" value="text/html" />
         <beans:entry key="json" value="application/json" />
     </beans:map>
</beans:property>

答案 1 :(得分:0)

为了让JSONSpring MVC应用程序中运行,您需要做的第一件事就是添加这些依赖项:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.1.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
        <scope>compile</scope>
    </dependency>

Spring需要这些依赖关系来管理JSON请求和响应。

接下来是通过注册ContentNegotiationManagerFactoryBean来定义媒体类型:

<bean id="contentNegotiationManager" 

class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="false"/>
        <property name="favorParameter" value="true"/>
        <property name="mediaTypes">
            <value>
                json=application/json
                xml=application/xml
            </value>
        </property>
    </bean>

此外,您必须在mvc:annotation-driven标记的属性content-negotiation-manager中定义此谈判经理:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jsonObjectMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

创建您的JSON对象映射器或使用默认值。我更喜欢创建自己的,以便我可以管理我需要的配置。例如:

public class JsonObjectMapper extends ObjectMapper {

    public JsonObjectMapper() {
        super();
        this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
        this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

}

在Spring上下文中声明它:

<bean id="jsonObjectMapper" class="somepackage.JsonObjectMapper"/>

然后你的javascript应该是这样的:

$(document).ready(function(){

    var obj = {};
    obj.name = "test";
    obj.id = "test";
    obj.type = "test";
    var request = {};
    request.SearchRequest = obj;

    $.ajax({
        url: "${pageContext.servletContext.contextPath}/find",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(request),
        contentType: 'application/json'
    }).success(
        function (data) {
            //do something with response
        });
});

如果我没有忘记别的东西,这应该有效。希望这会有所帮助。