【问题标题】:Server 415 Response code服务器 415 响应代码
【发布时间】:2012-01-31 18:22:12
【问题描述】:

我正在使用 Jetty Web 服务器和 Jersey 进行 REST 处理。

我定义了:

@POST
@Path("/sendMessage")
@Consumes ({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
public Response sendMessage(@Context final UriInfo uriInfo) 
{
    logger.debug("sendMessage:");
    System.out.println("Received POST!");
    return Response.status(Response.Status.OK).build();

}

但是,当我发送 http 请求 http://localhost:8080/hqsim/sendMessage 时,服务器会返回 415 代码。

这就像不允许调用。我该如何解决这个错误?

【问题讨论】:

    标签: java jersey jetty jax-rs http-request


    【解决方案1】:

    415 表示媒体类型不受支持。 最可能的情况是您的请求中缺少 Content-Type 标头,或者它不正确。在您的情况下,它必须是 application/xml 或 text/xml。

    【讨论】:

      【解决方案2】:

      如果你正在使用 axios,并且正在制作;

      a) 一个 post 请求,你应该如下定义请求

      await axios.post("the url you're speaking to",
           {the data to post}, 
           {
              headers: {"Content-Type": "application/json"}
           })
      

      b) 获取请求;

      await axios.get("the url you're speaking to",
           {
              data: {},
              headers: {"Content-Type": "application/json"},
              params: {'varX': '34'}
           })
      

      其中 varX 是您与请求一起发送的变量的名称 如果您不发送查询字符串,params 也可以为空。

      因此,该网址将显示为;

      https://myurl.com/?varX=34 
      

      【讨论】:

        猜你喜欢
        • 2018-04-24
        • 1970-01-01
        • 2017-08-22
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        相关资源
        最近更新 更多