繁体   English   中英

Spring 4,JSON-HTTP错误406不可接受

[英]Spring 4, JSON - HTTP Error 406 Not Acceptable

我已经为fullCalendar eventSource创建了控制器,不幸的是出了点问题,每次尝试打开此页面时,我都收到HTTP错误406。

我的控制器:

@RequestMapping(value="/schedule", method = RequestMethod.GET)
public @ResponseBody Object getEventyJSON(Principal principal){
    String name = principal.getName();
    List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();

    List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();

    for (int i = 0; i < eventy.size(); i++) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("title", eventy.get(i).getTitle());
        map.put("start", eventy.get(i).getStart());
        events.add(map);
    }

    return events;
}

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

当然,我已经尝试将依赖关系更改为codehaus而不是fasterxml,并且它不起作用。 我也尝试向方法添加一些标题。

当我将控制器更改为此:

@RequestMapping(value="/schedule", method = RequestMethod.GET)
    public @ResponseBody Object getEventyJSON(Principal principal){
        String name = principal.getName();
        List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();


        return "schedule";
    }

它正在工作,当我打开该页面时,它的打印计划是正确的。 我想念什么?

当服务器(后端)无法使用请求中指定的Accept-Type HTTP标头响应时,会发生406。 因此,基本上,您的服务器返回的是客户端请求Accept-Header中的不同响应类型。

由请求标识的资源仅能够生成响应实体,该响应实体具有根据请求中发送的接受标头不可接受的内容特征。

在这里检查

在通话中,您需要告诉资源您期望的内容类型

如果您使用的是JQuery,则可以执行以下操作

$.ajax({
    type: "GET",
    url: "/schedule",
    contentType: "application/json",
    dataType : "json",
    success: function (d) {
        // do something with the json
    }
});

尝试这个:

@RequestMapping(value="/schedule", produces= "application/json")
public @ResponseBody List<Eventy> getEventy(Principal principal){

尝试更改您的方法以返回String,然后在您的方法内将列表转换为JSON String。 为此,您可以使用google-gson,但是,鉴于我们已经在使用spring,并且项目中可能已经具有这些依赖项,则可以使用Jackson的ObjectMapper。 像这样:

    @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {
        // NOTE1: change the method to return a "String"
        // NOTE2: add "throws JsonProcessingException" to the method (or catch it)
    String name = principal.getName();
    List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();
    List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();

    for (int i = 0; i < eventy.size(); i++) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("title", eventy.get(i).getTitle());
        map.put("start", eventy.get(i).getStart());
        events.add(map);
    }
    //return events;
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(events);
}

还要添加以下两个导入:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

我很确定以上内容将为您解决。 但是,如果以上方法不能解决您的406错误响应,请尝试另一件事,将其添加到@RequestMapping中:

headers="Accept=*/*",produces = "application/json"

例如:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")

尝试更改您的方法以返回String,然后在您的方法内将列表转换为JSON String。 为此,您可以使用google-gson,但是,鉴于我们已经在使用spring,并且项目中可能已经具有这些依赖项,则可以使用Jackson的ObjectMapper。 像这样:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {
    // NOTE1: change the method to return a "String"
    // NOTE2: add "throws JsonProcessingException" to the method (or catch it)
    String name = principal.getName();
    List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();
    List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();

    for (int i = 0; i < eventy.size(); i++) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("title", eventy.get(i).getTitle());
        map.put("start", eventy.get(i).getStart());
        events.add(map);
    }
    //return events;
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(events);
}

还要添加以下两个导入:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

我很确定以上内容将为您解决。 但是,如果以上方法不能解决您的406错误响应,请尝试另一件事,将其添加到@RequestMapping中:

headers="Accept=*/*",produces = "application/json"

例如:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/js

您在请求映射中需要这个

@RequestMapping(value="/schedule", method=RequestMethod.GET, produces="application/json")

您可能还需要这些依赖项

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

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

我认为您的pom.xml中缺少依赖项

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM