0
votes

Dears,

I am sending a post request from Angular 2 to a Java service using Jersey .

Angular code :

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

Form Input used :

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

Jersey code :

@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";
    }

Data being sent is : {"effort": "208", "crc": "123"}

I am getting the error "415 -Unsupported media type" in Angular application

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

Could you please spot the issue.

Update :

I am using the Jersey JAX-RS 2.0 RI bundle .

I am already using genson as JSON provider.

Moreover, the same post call works from postman but is not working and giving error from Angular 2 http service call.

1
Why are you trying to use @FormParam? You need to use a POJO for the entity body paramPaul Samsotha
To get the values submitted from the form.Kalyan Chavali
But that's not how you get it from JSON. That is only used for application/x-www-form-urlencoded data. If you change the client to send that type of data, then it should work. Otherwise, use a POJO on the server side.Paul Samsotha
If it still doesn't work when you use the POJO, please update your post to show how you did it, and also show your Maven/Gradle dependencies (related to Jersey/Jackson)Paul Samsotha
I tried both and but still didnt work.Kalyan Chavali

1 Answers

0
votes

Dears, I have found the issue with the code. The problem is with both the POST method and the angular HTTP call .

1) The @POST method has been changed to below

    @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";
    }

and the headers are now sent properly in the angular http post call as below as an object instead of a variable

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