简体   繁体   English

JAX-RS @POST语法

[英]JAX-RS @POST Syntax

I have not used @POST before and am not sure about the syntax and how to test. 我之前没有使用@POST ,也不确定语法和测试方法。 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 . 我想改变@GET@POST Need help with syntax and how to test the WS call through a browser. 需要有关语法的帮助以及如何通过浏览器测试WS调用。

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. POST与GET根本不同,并且在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. 然后,您需要使用JAX-RS提供程序(RESTEasy,Spring MVC,Restlet等)来对JID(通常使用Jackson)执行CandidateObject POJO的自动反序列化,这将使getter和setter映射到原始查询中的字段你的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. 您还将拥有ClassroomAssignment POJO,并且您的JSON序列化程序(通常也是Jackson)会将该POJO转换为JSON。

But ultimately, if a GET worked before, you should probably keep things as GET. 但最终,如果GET之前工作过,你应该把事情保持为GET。 Just rethink your design and how the GET call is made. 只需重新考虑您的设计以及如何进行GET调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM