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.
@FormParam
? You need to use a POJO for the entity body param – Paul Samsotha