user1810305
user1810305

Reputation: 23

JAX-RS @POST Syntax

I have not used @POST before and am not sure about the syntax and how to test. Here's what I have now:

@GET
@Path("classroomAssignmentId/{classroomAssignmentId}/classroomId/{classroomId}/assignmentName/{assignmentName}/assignmentDesc/{assignmentDesc}/assignmentDueDt/{assignmentDueDt}/assignmentDocument/{assignmentDocument}/assignmentStatusId/{assignmentStatusId}/updatedBy/{updatedBy}")
@Produces(MediaType.APPLICATION_JSON)


public ClassroomAssignment getCandidatesAsJson( @PathParam("classroomAssignmentId") int classroomAssignmentId
                        ,@PathParam("classroomId") int classroomId
                        ,@PathParam("assignmentName") String assignmentName
                        ,@PathParam("assignmentDesc") String assignmentDesc
                        ,@PathParam("assignmentDueDt") String assignmentDueDt
                        ,@PathParam("assignmentDocument") String assignmentDocument
                        ,@PathParam("assignmentStatusId") int assignmentStatusId
                        ,@PathParam("assignmentTypeId") int assignmentTypeId
                        ,@PathParam("updatedBy") String updatedBy)

I want to change the @GET to @POST. Need help with syntax and how to test the WS call through a browser.

Upvotes: 0

Views: 147

Answers (1)

Vidya
Vidya

Reputation: 30300

There are two issues here.

First, merely wanting to express your parameters differently is insufficient for changing the semantics of your call. A POST is fundamentally different from a GET, and both semantics are very clearly defined in REST. You shouldn't switch just for convenience sake.

But second, if you find the theory pedantic and just care about how to practically get this done, you will use something like this:

@POST
@Path("/classroom-assignment)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ClassroomAssignment getCandidatesAsJson(CandidateObject myObject) {
...
}

Then you will need your JAX-RS provider (RESTEasy, Spring MVC, Restlet, etc.) to perform automatic deserialization of JSON (typically with Jackson) to the CandidateObject POJO, which will have getters and setters mapping to the fields in your original query string of your GET.

At least that's the default serialization, which will be sufficient and easiest.

You will also have a ClassroomAssignment POJO, and your JSON serializer (again typically Jackson) will convert that POJO to JSON.

But ultimately, if a GET worked before, you should probably keep things as GET. Just rethink your design and how the GET call is made.

Upvotes: 2

Related Questions