406 Spring MVC Json,根据请求“接受”标头不可接受
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26615416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
406 Spring MVC Json, not acceptable according to the request "accept" headers
提问by bekur
have following details in my pom.xml
在我的 pom.xml 中有以下详细信息
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-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:
应用程序-config.xml:
<context:component-scan base-package="com.test" />
<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler /> -->
<mvc:resources mapping="/resources/**" location="/resources/" />
JSP page:
JSP页面:
<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/syncAccountsthis returns 406 and description is:
单击按钮后,它返回 url,因为
http://lt-50k7sy1:8080/QuickBooks-UX/syncAccounts它返回 406,描述是:
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.
我已关注此链接,但没有结果。
回答by TechFind
Add following jar to your pom.xml file which is required for Spring 4.1.*
将以下 jar 添加到 Spring 4.1.* 所需的 pom.xml 文件中
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
回答by Ajay Kumar
Get rid of thisissues by adding @EnableWebMvcin Controller class.
通过在 Controller 类中添加@EnableWebMvc来摆脱这个问题。
@Controller
@RequestMapping("/API/course")
@EnableWebMvc
public class CourseController {
@Autowired
private com.item.DAO.CourseRepository courseRepository;
@ResponseStatus(value=HttpStatus.OK)
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody List<Course> getListOfCourse(){
List<Course> courses = courseRepository.getListOfCourse();
return courses ;
}
or add following line in xml configuration file if XML configuration is being used in your project.
或者,如果您的项目正在使用 XML 配置,则在 xml 配置文件中添加以下行。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<bean>
and add following dependencies in your pom.xml file:-
并在您的 pom.xml 文件中添加以下依赖项:-
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.4.1.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
回答by Nirbhay Rana
Use below dependency in your pom working with spring 4.
在使用 spring 4 的 pom 中使用以下依赖项。
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.5.3</version>
</dependency>
回答by Dai Kaixian
Maybe all fields of your POJO need Getter and Setter.
也许你的 POJO 的所有字段都需要 Getter 和 Setter。
I fixed it according to this issue: reference:Spring MVC - HttpMediaTypeNotAcceptableException
我根据这个问题修复了它:参考:Spring MVC - HttpMediaTypeNotAcceptableException
And 406 is not a useful message to fix the bug. You should debug into the codes and see what the Exception is on earth.
并且 406 不是修复错误的有用消息。您应该调试代码并查看异常是什么。
回答by zipzapsam
Thanks it solved my issue. I finally ended up with a pom.xml like this:
谢谢它解决了我的问题。我最终得到了这样的 pom.xml:
<!-- Json dependency -->
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
<!-- Json dependency -->
回答by Clement Amarnath
It worked for me with the below dependencies and @EnableWebMvcin my RestController, Please see that I just added Hymanson-databinddependency.
它使用以下依赖项和@EnableWebMvc我的 RestController 对我有用,请注意我刚刚添加了Hymanson-databind依赖项。
<properties>
<spring-version>4.2.1.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>

