简体   繁体   English

尝试进行JSON调用时出现415(不支持的媒体类型)错误

[英]415 (Unsupported Media Type) error while trying to make a JSON call

I get this error while I am making a rest call. 我打个电话时收到此错误。

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)

This is my javascript function(#showdata is id of div where I will display String data): 这是我的javascript函数(#showdata是div的ID,我将在其中显示字符串数据):

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

And this is the java code which calls for the service (another java code) 这是调用服务的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/");
    }

}

Service side code function: 服务端代码功能:

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

Add "Content-Type: application/json" and "Accept: application/json" in REST Client header section 在REST Client标头部分中添加“ Content-Type:application / json”和“ Accept:application / json”

or 要么

Since your code is trying to work with JSON, are you sure you have registered the class in Jackson? 由于您的代码正在尝试使用JSON,因此您确定已在Jackson中注册了该类吗? By default JAXB would enable serialisation to and fro XML, but for JSON, you need to include Jackson. 默认情况下,JAXB将启用XML来回序列化,但是对于JSON,您需要包括Jackson。

More info here: https://jersey.java.net/documentation/latest/media.html#json.jackson 此处提供更多信息: https : //jersey.java.net/documentation/latest/media.html#json.jackson

Do this, it will work by adding this filter: 这样做,将通过添加以下过滤器来起作用:

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() {}

}

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

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