请帮帮我。我尝试了很长时间来启动rest应用示例,但是我做不到。
我使用jersey user guide陷入困境,这里是示例:
package com.example;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.glassfish.grizzly.http.server.HttpServer;
...
public class MyResourceTest {
private HttpServer server;
private WebTarget target;
@Before
public void setUp() throws Exception {
server = Main.startServer();
Client c = ClientBuilder.newClient();
target = c.target(Main.BASE_URI);
}
@After
public void tearDown() throws Exception {
server.stop();
}
/**
* Test to see that the message "Got it!" is sent in the response.
*/
@Test
public void testGetIt() {
String responseMsg = target.path("myresource").request().get(String.class);
assertEquals("Got it!", responseMsg);
}
}
但我不知道,带有startServer()方法的Main类是什么?这里没有此类的导入。
最佳答案
这是Main类的link。
Main.startServer()看起来像这样:
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in $package package
final ResourceConfig rc = new ResourceConfig().packages("$package");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
关于java - 无法开始使用jersey用户指南,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20744421/