【发布时间】:2020-07-30 07:35:44
【问题描述】:
我只想创建一个简单的 REST 服务,它使用 @GET 和 @POST。
对于@GET 函数,一切正常,但对于@POST,当我想在我的服务器上创建一个新用户时,浏览器只是保持饱和(不允许使用方法)。
我阅读了很多关于如何解决此错误的文章,但我还没有得到任何东西。
我的@POST 代码:
@Path("/hello")
public class HelloResource(){
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/post")
public Response createUser(@PathParam("name") String name,@PathParam("address") String address,@PathParam("birthYear") String birth,@PathParam("ps") String password) throws NotAllowedException,MethodNotFoundException,Exception {
DataStore.getInstance().putPerson(new Person(name, address, Integer.parseInt(birth), password));
String json = "{\n";
json += "\"status\": " + '"'+"CREATED" +'"'+ ",\n";
json+="}";
return Response.status(200).entity(json).build();
}}
我还尝试使用 (MediaType.APPLICATION.JSON) 和 (MediaType.TEXT_PLAIN) 添加 @Consumes 函数,但没有任何改变。
我输入的发帖网址是:
http://localhost:8080/HelloREST/rest/hello/post?name=PouYad&address=mustbejsonlater&birthYear=2005&ps=12345
如你所见,我也尝试了很多异常处理程序。
有人可以帮忙吗?
【问题讨论】:
-
您实际上是在发送 POST 请求吗?尝试添加 @GET 处理程序。我猜您实际上是在向您的应用程序发送 GET 请求;在这种情况下,“方法不允许”将是预期的错误,因为您只有一个 POST 处理程序。
-
您使用了错误的注释。对于那些使用
@QueryParam,而不是@PathParam。 -
@GabrielBauman。什么意思
-
你可以试试 Postman 工具吗?将 http 方法设置为 post 并尝试。
-
@PooyaSalehi 我的意思是您可能正在向端点发送 GET 请求而不是 POST。错误正是告诉你这一点。
标签: java rest http-status-code-405