406 Spring MVC Json,根据请求“接受”标头不可接受

在我的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页面:

 <form:form method="POST" action="/QuickBooks-UX/syncAccounts">

<input type="submit" value="Sync Account"/>

</form:form>

控制器:

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

}

}

我正在以以下方式调用我的网址:

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

单击按钮后,它返回url,因为 http://lt-50k7sy1:8080/QuickBooks-UX/syncAccounts它返回406,描述为:

该请求标识的资源只能根据请求“接受”标头生成特性不可接受的响应

回答:

将以下jar添加到Spring 4.1。*所需的pom.xml文件中。

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

以上是 406 Spring MVC Json,根据请求“接受”标头不可接受 的全部内容, 来源链接: utcz.com/qa/406378.html

回到顶部