Angular 2 - 发布请求 - 向Jersey发送请求时不支持的媒体类型

时间:2017-03-13 05:57:19

标签: java rest angular jersey jax-rs

逗人,

我正在使用Jersey向Angular 2发送一个帖子请求。

角度代码:

 saveProjectDetails(info: any) {
    const body = JSON.stringify(info);
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    alert(body);
    return this.http.post(this.postUrl, body, headers).map((response: Response) => response.text());
  }

使用的表格输入:

<div class="form-group">
    <div class="row">
      <div class="col-lg-4">
        <input class="form-control" or="effort" name="effort" placeholder="Enter effort" value="208" #effort>
      </div>
      <div class="col-lg-4">
        <input class="form-control" for="crc" name="crc" placeholder="Enter CRC" value="CRC06" #crc>
      </div>
      <div class="col-lg-4">
        <button class="form-control" id="Save" class="btn btn-primary" (click)="onSubmit(effort.value, crc.value)">Save project details</button>
      </div>
    </div>
  </div>

  <p> Response obtained from server is : {{projectSaveResponse}}</p>

泽西岛代码:

@POST
    @Path("/saveProjectInfo/{projectId}/{application}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    @JsonRawValue
    public String saveProjectInfo(@PathParam("projectId") String projectId,
            @PathParam("application") String application,
            @FormParam("effort") String effort, @FormParam("crc") String crc,
            @Context HttpServletRequest request) {

        System.out.println("Effort provided : " + effort);
        System.out.println("CRC provided : " + effort);

        return "Project description saved successfully";
    }

正在发送的数据是:{"effort": "208", "crc": "123"}

我在Angular应用程序中收到错误“415 -Unsupported media type”

POST http://localhost:8080/ProjectDashboard/rest/jsonServices/saveProjectInfo/134001D01/OPTA 415 (Unsupported Media Type)

请你发现问题。

更新:

我正在使用泽西岛JAX-RS 2.0 RI套装。

我已经将genson用作JSON提供程序。

此外,同样的邮件调用从邮递员工作,但不工作,并从Angular 2 http服务调用给出错误。

1 个答案:

答案 0 :(得分:0)

亲爱的,我发现了代码的问题。问题在于POST方法和角度HTTP调用。

1)@POST方法已更改为

以下
    @POST
    @Path("/saveProjectInfo/{projectId}/{application}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @JsonRawValue
    public String saveProjectInfo(SaveBean input,
            @PathParam("projectId") String projectId,
            @PathParam("application") String application) {

        System.out.println("ProjectId provided : " + projectId);
        System.out.println("Application provided : " + application);
        System.out.println("Effort : " + input.getEffort());
        System.out.println("CRC : " + input.getCrc());

        return "Project description saved successfully";
    }

现在可以在角度http post调用中正确发送标题,如下所示,而不是变量

saveProjectDetails(info: any) {
    const body = JSON.stringify(info);
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    alert(body);
    return this.http.post(this.postUrl, body, {headers: headers}).map((response: Response) => response.text());
  }