0

我有两种控制器方法

@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”

4

2 回答 2

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>
于 2013-09-06T09:30:53.833 回答
0

为了JSON在您的Spring 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
        });
});

如果我没有忘记别的东西,这应该可以工作。希望这可以帮助。

于 2013-09-06T10:15:38.663 回答