有什么方法可以检查 JAX-RS/Java EE 应用程序何时启动/部署?
此时,我想检查数据库是否已初始化。否则,只有在数据库没有初始化的情况下,才需要进行初始化,并且需要在Java EE应用程序启动时进行检查。
因此,我需要知道是否有任何方法可以在 JAX-RS/Java EE 应用程序启动时进行捕获。
有任何想法吗?
我正在从一个基本的 Java 程序向一个 URL 发出 HTTP GET 请求,它恰好是"http://localhost:9992/users/john.doe@example.com":
public class Tester {
public static void main(String[] args) {
HttpRequester.doesUserExist("john.doe@example.com");
}
}
Run Code Online (Sandbox Code Playgroud)
的实现HTTPRequester是这样的:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequester {
private static HttpURLConnection connection = null;
public static boolean doesUserExist(final String email) {
final String targetUrl = Constants.URL_USER_SRVC + email;
System.out.println(targetUrl);
try {
URL url = new URL(targetUrl);
connection = (HttpURLConnection) url.openConnection();
System.out.println(connection.getRequestMethod());
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoOutput(true); …Run Code Online (Sandbox Code Playgroud) 我编写了一个类扩展 ContainerRequestFilter 以进行权限检查。如何获取匹配方法的注释以进行权限检查。
@javax.ws.rs.ext.Provider
public class AuthorizationRequestFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
// how can I get resources' methods' annotation here?
// from the resource below , I want to checkout whether the target matched method contains the @ReadPermission annotation
}
}
@Path("/region")
public class Region {
@POST
@Path("/{region_id}")
@Produces({MediaType.APPLICATION_JSON , MediaType.APPLICATION_XML})
@ReadPermission
public String getRegion() {
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义ExceptionMapper,每次 API 中发生异常时我都会调用该自定义,以将其映射到合适的响应。以下是我的自定义异常类:
@Provider
public class ServiceExceptionMapper implements ExceptionMapper<Throwable> {
private Logger logging = LoggerFactory.getLogger(getClass());
@Override
public Response toResponse(Throwable throwable) {
log.error("There is an exception: ", throwable);
if (throwable instanceof IllegalArgumentException) {
return Response.status(Response.Status.BAD_REQUEST).entity(throwable.getMessage()).type (MediaType.TEXT_PLAIN).build();
}
if (throwable instanceof WebApplicationException) {
WebApplicationException we = (WebApplicationException) throwable;
return we.getResponse();
}
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(throwable.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的资源类中,我有一个 try 和一个 catch 块。如果有异常,catch 块应该捕获它并调用自定义异常映射器类。抛出异常的常用方法如下:
catch (Exception e) {
throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Internal Server Error").build());
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过以下方式调用异常映射器类:
catch (Exception e) {
exceptionMapper.toResponse(e);
}
Run Code Online (Sandbox Code Playgroud)
exceptionMapper类的字段在哪里 …
我想制作一个网络服务,您可以在其中查询通用数据。所以,我的网址将是这样的:
.../field_name/Country/field_value/US/field_name/City/field_value/Boston
用 Jersey 读取它的方法是什么 - 我想获得一个字段名称数组和一个相应的字段值数组。
我正在使用 Jackson 来实现一个简单的 REST API。因为这是第一次,我想确保我遵循正确的做法。
查看各种示例,我发现在 Jackson 库中实现了注释,例如 @JsonProperty。我还发现了 jax-rs 中定义的其他注释。
我不清楚 Jackson 何时结束,jax-rs 何时开始,反之亦然。可以使用这两个注释来实现 API 吗?是否存在重叠或总是用于定义 API 的不同特性?
我想在出现错误时中止请求并返回 JSON。但下面的代码只是返回一个字符串说"Unexpected 'U'"(这不是我的信息)
我如何让它通过 JSON 响应中止?
requestContext.abortWith(
Response.status( Response.Status.UNAUTHORIZED ).entity(
e.getMessage()
).type(
MediaType.APPLICATION_JSON
).build()
);
Run Code Online (Sandbox Code Playgroud) 我知道这个问题有很多答案,但我仍然对 JAX-RS API(规范)和 Jersey 框架(参考实现)之间的区别感到困惑。
我读到:
Jersey 框架基本上使用 com.sun.jersey.spi.container.servlet.ServletContainer servlet 来拦截所有传入的请求。当我们在项目 web.xml 中配置时,所有传入的休息请求都应该由该 servlet 处理。有一个 init-param 配置了 jersey servlet 以查找您的 REST 服务类。REST 服务类不是 Servlet,它们不需要像您在代码中所做的那样扩展 HttpServlet。这些 REST 服务类是简单的 POJO 注释,用于告诉 jersey 框架有关不同属性的信息,例如路径、消费、生产等。当您从服务方法返回时,jersey 负责在定义的“PRODUCES”响应类型中编组这些对象并编写在客户端流上
我的问题是,当您说:“球衣负责在定义的 'PRODUCES' responseType 中编组这些对象并将其写入客户端流中”,您所说的球衣是什么意思,处理对象的实际类或库是什么。
当我读到 jersey 是处理 JAX-RS API 规范的引擎时,我感到很困惑。有人可以解释一下这句话中 jersey 一词背后的确切含义吗?泽西岛的哪个实际类在泽西岛完成处理请求和响应的工作?
我正在使用MediaType.APPLICATION_FORM_URLENCODED_TYPE 中的“resteasy-client”库发送 POST 请求。
示例代码:
String serviceUrl = "URL";
ConnectRequest connectRequest = new ConnectRequest();
connectRequest.setUsername("");
connectRequest.setPassword("");
connectRequest.setScope("bearer");
connectRequest.setGrant_type("");
Entity<ConnectRequest> entity = Entity.entity(connectRequest,
MediaType.APPLICATION_FORM_URLENCODED_TYPE);
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(serviceUrl);
Response response = target.request().post(entity);
System.out.println("RESP : "+response.toString());
Run Code Online (Sandbox Code Playgroud)
Maven 依赖项
<properties>
<java.version>1.7</java.version>
<resteasy.version>3.0.4.Final</resteasy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>${resteasy.version}</version> …Run Code Online (Sandbox Code Playgroud) 在以下对象中:
class Foo implements Serializable {
transient String bar;
String baz;
}
Run Code Online (Sandbox Code Playgroud)
JAX-RS 将忽略瞬态关键字并仍然bar在响应中进行序列化。在这种情况下,要阻止它被序列化,您需要使用注释(即@XmlTransient)。这个要求背后的原因是什么?从表面上看。似乎只有关键字就足够了。
在哪些用例中关键字可能不足/不正确并且需要注释?
jax-rs ×10
java ×8
rest ×5
web-services ×4
jakarta-ee ×2
jersey ×2
jersey-2.0 ×2
cxf ×1
jackson ×1
json ×1
maven ×1
permissions ×1
resteasy ×1
transient ×1