简体   繁体   中英

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

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)

@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

or

Since your code is trying to work with JSON, are you sure you have registered the class in Jackson? By default JAXB would enable serialisation to and fro XML, but for JSON, you need to include Jackson.

More info here: 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() {}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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