Spring MVC& JSON - 406不可接受@Responsebody

时间:2017-01-10 19:49:51

标签: json spring-mvc

我想在控制器中使用@ResponseBody生成一个json对象。但我在浏览器端获得的是:状态406“此请求标识的资源只能根据请求”接受“标题生成具有不可接受特征的响应。”

下面是pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.anand</groupId>
  <artifactId>AnandWebStore</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>AnandWebStore Maven Webapp</name>
  <url>http://maven.apache.org</url>

 <properties>
        <spring.version>4.0.3.RELEASE</spring.version>
        <hibernate.core>4.2.7.Final</hibernate.core>
    </properties>

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>  


         <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-orm</artifactId>
               <version>${spring.version}</version>
         </dependency>
         <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-jms</artifactId>
               <version>${spring.version}</version>
         </dependency>


        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.2.GA</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>


        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>

        <dependency>
           <groupId>commons-fileupload</groupId>
           <artifactId>commons-fileupload</artifactId>
           <version>1.3</version>
        </dependency>

        <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.core}</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4-2.0</version>
        </dependency>

         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.webflow</groupId>
            <artifactId>spring-js-resources</artifactId>
            <version>2.4.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.7.4</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.7.4</version>
        </dependency>



  </dependencies>
  <build>
    <finalName>AnandWebStore</finalName>
  </build>
</project>

我使用了jackson-core-asl&amp; jackson-mapper-asl版本为1.7.4。

以下是jsp中将数据发布到控制器的一段代码。

<form:form class="well form-horizontal" action="saveCustomer.html" method="post"  id="contact_form" commandName="customer">
        <fieldset>

            <div class="form-group">
                <label class="col-md-4 control-label">First Name</label>  
                <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        <form:input  path="first_name" name="first_name" placeholder="First Name" class="form-control"  type="text" />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label" >Last Name</label> 
                <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        <form:input path ="last_name" name="last_name" placeholder="Last Name" class="form-control"  type="text" />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label">E-Mail</label>  
                <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                        <form:input path="email" name="email" placeholder="E-Mail Address" class="form-control"  type="text" />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label">Phone #</label>  
                <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
                        <form:input path ="phone" name="phone" placeholder="(845)555-1212" class="form-control" type="text"/>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label">Address</label>  
                    <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
                        <form:input path="address" name="address" placeholder="Address" class="form-control" type="text"/>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label">City</label>  
                <div class="col-md-4 inputGroupContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
                        <form:input path="city" name="city" placeholder="city" class="form-control"  type="text"/>
                    </div>
                </div>
            </div>

            <div class="form-group"> 
                <label class="col-md-4 control-label">State</label>
                <div class="col-md-4 selectContainer">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
                        <form:select path="state" name="state" class="form-control selectpicker">
                            <option value=" " >Please select your state</option>
                            <option>......</option>
                        </form:select>
                    </div>
                </div>
            </div>

            <div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Customer Added Successfully...</div>

            <div class="form-group">
                <label class="col-md-4 control-label"></label>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-warning" >Add <span class="glyphicon glyphicon-send"></span></button>
                </div>
            </div>

        </fieldset>
    </form:form>

以下是调度程序servlet的代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    ">

    <mvc:annotation-driven/>
    <mvc:resources location="/resources/" mapping="/resources/**" />
    <context:component-scan base-package="com.anand" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
    </bean>

</beans>

我在这里使用了<mvc:annotation-driven/>

下面是控制器的代码:

package com.anand.controllers;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.anand.forms.CustomerForm;
import com.anand.forms.LoginForm;
import com.anand.model.Customer;
import com.anand.services.CustomerService;

@Controller
public class CustomerController {

    private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value="saveCustomer.html",method=RequestMethod.POST)
    private @ResponseBody Map<String, Object>saveCustomer(@Valid Customer customer, BindingResult result)
    {
        if(result.hasErrors())
        {

        }
        boolean saveFlag = customerService.save(customer);
        Map<String, Object> map = new HashMap<String, Object>();
        if(saveFlag==true)
        {
            map.put("status", 200);
            map.put("Message", "Data for customer saved successfully");
        }
        else
        {
            map.put("status", 500);
            map.put("Message", "Error occured at server side");
        }
        return map;
    }
}

注意:在这里我可以看到来自UI的发布数据存储在数据库中,我可以看到在eclipse控制台中执行相同的hibernate查询,但在ui方面我得到错误406。 我已经分析了brwoser的网络部分,并找到了以下信息:

请求标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:126
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=A343E5E9FBF5866A12CEC2703DD5A5AE
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/AnandWebStore/forms/loginform.html
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

从stackoverflow中的几个问题的答案我知道问题是在下面的行,这个标题应该像application / json:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
是的,没关系。但是如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

这有点令人困惑 - 您想发送HTML表单POST请求并获取JSON作为响应?我真的建议您决定是否要使用表单和HTML响应或使用JSON来处理整个请求。它将更加清晰和用户友好

但是如果你真的想要使用表单并生成JSON(这对我来说没有意义),请在javascript中使用select dvd.dvd_id, dvd.dvd_title, rentalprices.daily_charge, dvd.no_of_discs, dvd.age_rating, publisher.publisher_name, distribution_company.company From dvd inner join rentalprices on dvd.rental_rate_id=rentalprices.daily_charge inner join publisher on dvd.publisher_id=publisher.publisher_name inner join distribution_company on dvd.dist_comp_id=distribution_company.company; 发送表单,或者使用exten => _X.,n,Set(CALLERID(num)=${somevar}) 之类的其他JS库并添加标题{ {1}}。无法使用HTML表单发送JSON请求。列出了接受的类型here

我建议使用和生成HTML,或生成和使用JSON。如果您决定使用JSON,请记住在控制器参数附近添加XMLHttpRequest注释。所以它看起来像

jQuery

希望有所帮助

答案 1 :(得分:0)

如果要返回json对象,则需要从控制器返回ResponseEntity:

return new ResponseEntity<Map<String, String>>(map, HttpStatus.OK);

答案 2 :(得分:0)

尝试这样的事情

@RequestMapping(value = "saveCustomer.html",
        method = RequestMethod.POST,
        consumes= { "application/json" },
        produces = { "application/json" })
    public ResponseEntity<String> saveCustomer(@Valid Customer customer, BindingResult result) {

    // your business logic here

    if(saveFlag==true) {
        return new ResponseEntity<String>("Data for customer saved successfully", HttpStatus.OK);   
    }
    else {
        return new ResponseEntity<String>("Error occured at server side", HttpStatus.INTERNAL_SERVER_ERROR);            
    }

}

答案 3 :(得分:0)

当您在Form POST上执行HTML Submit时,您需要定义enctype属性,如下所示,

<form enctype='application/json'>

这将允许您的用户代理处理json响应。

答案 4 :(得分:0)

最后它现在正在工作。首先我改变了做同样的方法。现在使用Ajax调用而不是使用表单提交,如下所示:

$('#AddCustButton').click(function(){

        var firstName = $('#first_name').val();
        var lastName = $('#last_name').val();
        var email = $('#email').val();
        var phone = $('#phone').val();
        var address = $('#address').val();
        var city = $('#city').val();
        var state = $('#state').val();


        $.ajax({
            url:'saveCustomer',
            type:'POST',
            dataType:'json',
            data:{'first_name':firstName,'last_name':lastName,'email':email,'phone':phone,'address':address,'city':city,'state':state},
            success:function(response){
                if(response.status=='Success'){
                    $('#success_message').slideDown({ opacity: "show" }, "slow"); // Do something ...
                    $('#contact_form').data('bootstrapValidator').resetForm();
                }
            }
        });
    });

然后检查了响应,但是状态代码406得到了同样的错误。然后我得到了将Codehus-Jackson库更改为FasterXml-Jackson的线索来自@Walfrat的评论(请参阅下面的问题评论)和来自保罗的污垢对这个问题的回答 Question

从codehaus-Jackson转到更快的XML-Jackson之后,它正在工作。