【问题标题】:415 (Unsupported Media Type) error while trying to make a JSON call尝试进行 JSON 调用时出现 415(不支持的媒体类型)错误
【发布时间】:2014-06-11 06:26:49
【问题描述】:

我在进行休息呼叫时收到此错误。

GET localhost:8082/abc/rest/hello/world 415 (Unsupported Media Type) jquery-    1.11.0.min.js:4
    n.ajaxTransport.send jquery-1.11.0.min.js:4
    n.extend.ajax jquery-1.11.0.min.js:4
    n.each.n.(anonymous function) jquery-1.11.0.min.js:4
    n.extend.getJSON jquery-1.11.0.min.js:4
    getExcelOutput utility.js:6
    (anonymous function)

这是我的 javascript 函数(#showdata 是 div 的 id,我将在其中显示字符串数据):

function getExcelOutput() {
    $.getJSON("/abc/rest/hello/world", function(data) { 
         $('#showdata').html("<p>item1="+data.val()+"</p>");
     });
    }

这是调用服务的java代码(另一个java代码)

@RequestScoped
public class ABCServiceImpl implements BasicService {

    @GET
    @Path("/hello/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public String hello(@PathParam("name") String name) {
        return generateProxy().hello(name);
    }

    private BasicService generateProxy() {
        return ProxyFactory.create(BasicService.class, "http://localhost:9090/service/lesson1/");
    }

}

服务端代码功能:

    @GET
    @Path("hello/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public String hello(String name)
    {
        return "Hello " + name + excelReader.excelReading(); 
    } 

【问题讨论】:

  • 它真的设置了响应的内容类型吗?
  • @JigarJoshi 抱歉,我是新手,你说的回复是什么意思?我正在发送呼叫的是服务端吗?
  • 当您收到服务器的响应时,内容类型设置为什么>
  • 尝试从网络浏览器点击localhost:8082/service/lesson1/abc/rest/hello/world。如果您收到响应,则表示呼叫存在问题,否则服务方面存在问题。也要发布服务端代码
  • @jsjunkie 当我输入时它是空白页。

标签: java javascript json spring resteasy


【解决方案1】:

在 REST Client 标头部分添加“Content-Type: application/json”和“Accept: application/json”

由于您的代码正在尝试使用 JSON,您确定您已在 Jackson 中注册了该类吗?默认情况下,JAXB 会启用与 XML 之间的序列化,但对于 JSON,您需要包含 Jackson。

更多信息在这里:https://jersey.java.net/documentation/latest/media.html#json.jackson

【讨论】:

    【解决方案2】:

    执行此操作,添加此过滤器即可:

    package filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Component;
    
    /**
     * 
     * @author Toni
     *
     */
    @Component
    public class SimpleCORSFilter implements Filter {
    
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
      }
    
      public void init(FilterConfig filterConfig) {}
    
      public void destroy() {}
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2018-10-13
      • 2018-07-25
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2017-08-28
      • 2014-04-29
      相关资源
      最近更新 更多