简体   繁体   English

Heroku JAX-RS开机自检

[英]Heroku JAX-RS POST

Scenario: 场景:

Recently, our team created an application on Heroku. 最近,我们的团队在Heroku上创建了一个应用程序。 We got all of our environments set up inside eclipse and became familiar with Git. 我们在eclipse中设置了所有环境,并熟悉了Git。 I was able to change some code and at least see that I could manipulate an http GET request to return the results that I wanted. 我能够更改一些代码,至少看到我可以操纵http GET请求以返回所需的结果。 My mission now, is to try to get POST working. 我现在的任务是尝试使POST正常工作。

Right now, we have a simple TestService where you can retrieve JSON objects from by doing something like this: myurl.com/services/test/test1 and it will return the JSON object: 现在,我们有一个简单的TestService,您可以在其中通过执行以下操作来检索JSON对象:myurl.com/services/test/test1,它将返回JSON对象:

{ "name": "test1", "test": 100 } {“ name”:“ test1”,“ test”:100}

Code: 码:

@Path("/test")
public class TestService
{
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public TestObject get()
    {
        return new TestObject();
    }

    @GET
    @Path("/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public TestObject get(@PathParam("name") String name)
    {
        return TestObject.getObject(name);
    }

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public void post(final TestObject object)
    {
        TestObject.postObject(object.getName(), object);
    }

}

Question: 题:

A) How to set up this code for an http POST request? A)如何为HTTP POST请求设置此代码?

B) How to actually request it like the above GET request? B)像上面的GET请求一样如何实际请求它?

A) If I get you right, you're looking for how to set up the POST method on your service. 答:如果我说对了,您正在寻找如何在服务上设置POST方法。 Try this, which is almost what you have right now. 试试看,这几乎是您现在拥有的。 Have a look at the return type which is javax.ws.rs.core.Response. 看一下返回类型是javax.ws.rs.core.Response。

@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public static Response post(
  String testObjectAsJson){      

  TestObject testObject = testObjectAsJson.toEntity; //some json deserialization function
  //do something with testObject


  //return a status of 200 to the client
  return Response.status(200).build;
}

If you want to process your testObject and return the modified one try this: 如果要处理testObject并返回修改的对象,请尝试以下操作:

String testObjectAsJson = testObject.toJson; //some json conversion method
return Response.status(200).entity(testObjectAsJson).build;

B) For testing your request eg use chrome://restclient/content/restclient.html in firefox. B)为了测试您的请求,例如在Firefox中使用chrome://restclient/content/restclient.html。 (maybe you have to add it first under add-ons). (也许您必须先在附加组件下添加它)。 There you can send POST requests to your service. 在那里,您可以将POST请求发送到您的服务。

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

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