Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

406 Spring MVC Json, not acceptable according to the request "accept" headers

have following details in my pom.xml

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

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>

application-config.xml:

<context:component-scan base-package="com.test" />
<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler /> -->
<mvc:resources mapping="/resources/**" location="/resources/" />

JSP page:

 <form:form method="POST" action="/QuickBooks-UX/syncAccounts">
        <input type="submit" value="Sync Account"/>
 </form:form>

Controller:

@Controller
@RequestMapping("/")
public class QuickBooksController {
    @RequestMapping(value = "/quickBooks", method = RequestMethod.GET)
    public String qucikBooks(ModelMap model) {
        logger.info("Welcome to QuickBooks controller");
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "quickBooks";
    }
    @RequestMapping(value ="/syncAccounts", method = RequestMethod.POST)
    public @ResponseBody List<SyncData> syncAccounts(@ModelAttribute("syncData")SyncData syncData, ModelMap model, BindingResult result) {
          List<SyncData> syncDataList = new ArrayList<SyncData>();
          try {

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet getRequest = new HttpGet(
                    "http://localhost:8292/qbsyncdata/getAccounts");
                getRequest.addHeader("accept", "application/json");

                HttpResponse response = httpClient.execute(getRequest);
                if (response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                       + response.getStatusLine().getStatusCode());
                }
                BufferedReader br = new BufferedReader(
                                 new InputStreamReader((response.getEntity().getContent())));                   
                while ((output = br.readLine()) != null) {
                    JSONParser jsonParser = new JSONParser();
                    JSONArray jsonArray = (JSONArray)jsonParser.parse(output);
                    for (Object object : jsonArray) {
                        JSONObject jsonObject = (JSONObject)object;
                        syncData = new SyncData();
                        syncData.setAccountName(jsonObject.get("accountName")==null?"":jsonObject.get("accountName").toString());
                        syncData.setAccountType(jsonObject.get("accountType")==null?"":jsonObject.get("accountType").toString());
                        syncData.setAccountSubType(jsonObject.get("accountSubType")==null?"":jsonObject.get("accountSubType").toString());
                        syncData.setActive(jsonObject.get("active")==null?"":jsonObject.get("active").toString());
                        syncDataList.add(syncData);
                    }                   
                    model.addAttribute("syncData", output);
                }
                httpClient.getConnectionManager().shutdown();
              } catch (Exception e) {
                e.printStackTrace();
              } 
            }
         return syncDataList;
    }
}

I am invoking my url as :

http://lt-50k7sy1:8080/QuickBooks-UX/quickBooks

After clicking the button, which returns the url as http://lt-50k7sy1:8080/QuickBooks-UX/syncAccounts this returns 406 and description is:

the resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers

I have followed this Link, but no results.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add following jar to your pom.xml file which is required for Spring 4.1.*

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...