如何使用JAX-RS获取服务器的基本URL?基本上我想要""http:// localhost:8080/.."当程序在localhost和"http://www.theSite.com/..."当程序在实时服务器上时.我正在使用泽西框架.
我有两个REST类用于简单的Web服务(Jersey和GlassFish),它涉及用户资源 - 一个用于操作所有用户(例如,@POSTing的工厂),另一个用于个人用户(例如@GET,@ PUT,@删除).他们在:
@Stateless @Path("users") public class AllUsersResource {...}
@Stateless @Path("user") public class OneUserResource {...}
Run Code Online (Sandbox Code Playgroud)
分别.在POST到AllUsersResource时,我想返回Response.created(uri).build()新用户的位置(通过),例如,
http://localhost:8080/.../user/152
Run Code Online (Sandbox Code Playgroud)
我的问题是如何做到这一点.AllUsersResource注入了@Context UriInfo uriInfo,但这并没有让我获得OneUserResource的@Path信息,只获得当前调用的信息("users").我最终使用它的方式只是使用反射,但我担心它是脆弱和不洁的:
OneUserResource.class.getAnnotation(Path.class).value();
Run Code Online (Sandbox Code Playgroud)
搜索StackOverflow我发现尝试的其他事情如下,但没有成功:
任何帮助都会很棒!
我有一个像这样的宁静的Web服务方法:
@GET
@Path("/generateInfo")
@Produces(MediaType.APPLICATION_JSON)
public String generateInfo(
@QueryParam("a") String a,
@QueryParam("b") String b,
@QueryParam("date") Date date) {
// ...business code...
return "hello world";
}
Run Code Online (Sandbox Code Playgroud)
如何从WebBrowser中调用该方法?问题是Date当我尝试给我404找不到或500内部服务器错误时的参数.
我有一个通用的JAX-RS资源类,我已经定义了一个泛型findAll方法
public abstract class GenericDataResource<T extends GenericModel> {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findAll() {
Query query = em.createNamedQuery(modelClass.getSimpleName()+".findAll");
List<T> list = query.getResultList();
return Response.ok(new GenericEntity<List<T>>(list) {}).build();
}
}
Run Code Online (Sandbox Code Playgroud)
和用户类:
public class User extends GenericModel {
...
}
Run Code Online (Sandbox Code Playgroud)
这里是示例子类定义:
@Path("users")
public class UserResource extends GenericDataResource<User> {
public UserResource() {
super(User.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
com.sun.jersey.api.MessageException: A message body writer for Java class
java.util.Vector, and Java type java.util.List<T>,
and MIME media type application/json was not found exception.
Run Code Online (Sandbox Code Playgroud)
如果我用定义的类替换T,例如User:
GenericEntity<List<User>>(list) …
我创建了一个在本地实现REST服务的应用程序:
Eclipse Indigo Jersey 2.4 Tomcat 7.0.47
使用Eclipse在本地运行时,服务工作正常,但在部署我的WAR文件时,尝试对其中一个服务URL执行GET时会出现以下异常:
HTTP Status 500 - Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
type Exception report
message Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
java.lang.Thread.run(Thread.java:662)
root cause
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:270)
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:218)
org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:448)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:300)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) …Run Code Online (Sandbox Code Playgroud) 我需要查看我的请求主体与客户端JAX-RS请求,以验证序列化是否正常,并重用请求测试客户端,如Postman.
我知道可以使用例如使用Jersey激活日志记录resource.addFilter(new com.sun.jersey.api.client.filter.LoggingFilter());.但是,我不直接使用Jersey或RESTEasy实现,而是通过JAX-RS API抽象:
final WebTarget target =
ClientBuilder.newBuilder().build().target("http://localhost:8080");
Run Code Online (Sandbox Code Playgroud)
如何在此处启用日志记录?
但是,有时close()来自代码片段的方法不会被JAX-RS实现调用(org.jboss.resteasy:resteasy-client-3.0.11.FINAL在本例中).
@FormDataParam和之间有什么区别@FormParam?
我@FormDataParam在一个方法中使用多个,但它抛出了媒体不支持的类型错误.但是当我使用时 @FormParam,我得到了价值观.
所以,我需要知道他们俩之间的区别是什么?
我很早就在REST实现中,并且最近了解到我们可以将JAX-RS注释放在我们的Java服务接口而不是类实现上.
对我来说,这似乎可能导致一个干净的类文件,但也可能导致开发人员不得不经常混淆文件.
每种方法的优缺点是什么?
Apache CXF(2.7.0)是否可以自动发现类路径中的JAX-RS资源?也就是说,带有注释的类@Path.
我在Spring应用程序中使用CXF,我必须使用以下XML手动声明资源,即使Spring已成功发现资源<context:component-scan ...>.
<jaxrs:server id="myService" address="/myService">
<jaxrs:serviceBeans>
<ref bean="myResource1" />
<ref bean="myResource2" />
<ref bean="myResource3" />
</jaxrs:serviceBeans>
</jaxrs:server>
Run Code Online (Sandbox Code Playgroud)
我想避免它(因为我可以使用其他JAX-RS实现,例如resteasy)因为在我的情况下它更难维护,并且它迫使我在Spring XML配置文件中声明我的bean依赖项.
我正在阅读"使用JAX-RS 2.0的RESTful Java"一书.我完全混淆了异步JAX-RS,所以我把所有问题都集中在一起.这本书写了这样的异步服务器:
@Path("/customers")
public class CustomerResource {
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public void getCustomer(@Suspended final AsyncResponse asyncResponse,
@Context final Request request,
@PathParam(value = "id") final int id) {
new Thread() {
@Override
public void run() {
asyncResponse.resume(Response.ok(new Customer(id)).build());
}
}.start();
}
}
Run Code Online (Sandbox Code Playgroud)
Netbeans创建这样的异步服务器:
@Path("/customers")
public class CustomerResource {
private final ExecutorService executorService = java.util.concurrent.Executors.newCachedThreadPool();
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public void getCustomer(@Suspended final AsyncResponse asyncResponse,
@Context final Request request,
@PathParam(value = "id") final int id) {
executorService.submit(new Runnable() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×7
rest ×5
jersey ×3
web-services ×2
annotations ×1
asynchronous ×1
cxf ×1
generics ×1
java-ee ×1
jersey-2.0 ×1
logging ×1
path ×1
spring ×1
tomcat7 ×1
url ×1