我希望元素在序列化为 XML 时具有不同的名称(例如“ fooXml”),而对于 JSON 则具有不同的名称(例如“ fooJson”)。是否可以?
我正在使用 XML 注释,例如:
@XmlElements({
@XmlElement(type = Foo.class, name = "fooXml"),
})
private SortedSet<Foo> fooSet;
Run Code Online (Sandbox Code Playgroud)
我已经尝试过@JsonProperty,但没有任何运气。
我还尝试将其导出到 getter 方法,例如:
@XmlElement(type = Foo.class, name = "fooXml")
@JsonProperty(value = "fooJson")
public List<Foo> getFooList() {
return new ArrayList<>(fooSet);
}
Run Code Online (Sandbox Code Playgroud)
但它总是忽略 JSON 注释并序列化为 XML 形式(fooXml名称)。
我该怎么做呢?
编辑:我正在使用 Jersey-json。
我想拦截任何class或methods注释为@Foo
类级别拦截:
@Foo
@path("/foo")
public class Attack {...}
Run Code Online (Sandbox Code Playgroud)
方法级拦截:
@path("/bar")
public class defend {
@Foo
@GET
public String myMethod(){....}
Run Code Online (Sandbox Code Playgroud)
我想拦截任何带有注释的类或方法,@Foo但不拦截其他方法或类。我想在继续方法执行之前打印出整个路径或 URI。一方法调用完成,我想打印出“执行成功”
这是这样的事情:
system.out.println(path) // this is the path the request is made. something like /api/2/imp/foo
method call happens
method call finishes
System.out.println("executed successfully")
Run Code Online (Sandbox Code Playgroud)
我的情况有所不同,但这是我遇到的根本问题。我不想具体实施。Java EE 7 规范有一种方法可以使用 @Postconstruct、@AroundInvoke 等来做到这一点。但我真的很难组装它。
这篇文章绝对是解决这个问题的好方法。但它是特定于实现的(RESTeasy)并且AcceptByMethod它使用的已被弃用。
谢谢
We used
org.glassfish.jersey.server.model.ResourceMethod$Builder
to register the method and the handler.
ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod);
methodBuilder.produces(restContext.getProduceContent()).handledBy(inflector);
methodBuilder.consumes(restContext.getConsumeContent()).handledBy(inflector);
Run Code Online (Sandbox Code Playgroud)
The handler class implements the org.glassfish.jersey.process.Inflector<ContainerRequestContext, Response>
public class CommonMethodInflector implements Inflector<ContainerRequestContext, Response>
{
@Override
public Response apply(ContainerRequestContext request)
{
//sync bloc
//using resqest object we do processing in different maner
incRestFeRequestCounters(request.getMethod());
Response response = processIncoming(request);`enter code here`
}
}
Run Code Online (Sandbox Code Playgroud)
Could you please help us in creating the async handler.
our requirement in short:
At runtime only we know the http method and other …
我试图获取 ContainerRequestFilter 对象中的方法注释。
控制器:
@GET
@RolesAllowed("ADMIN")
public String message() {
return "Hello, rest12!";
}
Run Code Online (Sandbox Code Playgroud)
容器请求过滤器:
@Provider
public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
//Here I need To get the @RolesAllowed("ADMIN") annotation value
}
Run Code Online (Sandbox Code Playgroud)
应用 :
@ApplicationPath("/rest")
public class ExpertApp extends Application {
private final HashSet<Object> singletons = new LinkedHashSet<Object>();
public ExpertApp() {
singletons.add(new SecurityInterceptor());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(UserControler.class, SearchController.class));
}
Run Code Online (Sandbox Code Playgroud)
}
网页.xml
<?xml …Run Code Online (Sandbox Code Playgroud) 例如,我有以下JAX-RS 1.1方法,该方法接收JWT 令牌,检查它,然后处理或拒绝请求,如下所示:
@GET
public Response getBook(@HeaderParam("Authorization") String authorizationToken, @QueryParam("id") String bookId) {
//if(authorizationToken is a valid JWT token)
// get User object which represented by this token
// get required book from database
// return 200 code with a book within response
//else if(authorizationToken is invalid by whatever reason it's)
// return 401 code within a response
//else if(database error)
// return 500 code within a response
}
Run Code Online (Sandbox Code Playgroud)
正如您在每个 jax-rs 方法中看到的,我需要使用相同的代码行:检查 token,将其转换为 …
是否可以在 a 中包含一条消息,BadRequestException以便当用户看到响应代码 a 400 时,他/她也能找出原因?
场景会是这样的,经过简化:
public Entity getEntityWithOptions(@PathParam("id") String id, @QueryParam("option") String optValue) {
if (optValue != null) {
// Option is an enum
try {
Option option = Option.valueOf(optValue);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
return new Entity(option);
}
return new Entity();
}
Run Code Online (Sandbox Code Playgroud)
我知道这可以通过返回一个对象来完成Response,但我不希望这样做。
这可能吗?也许有一个ExceptionMapper<BadRequestException>?或者这不能完成,因为BadRequestException已经是泽西岛特定的例外?
当我在 JAX-RS 中添加字符串的默认值时,它不采用该值。它保持为空或空。
@QueryParam("status")
private String status = "confirmed";
Run Code Online (Sandbox Code Playgroud)
当我将状态传递为空或null或未定义时,它保持为空或null或未定义。它不会将默认值视为已确认。
如何在使用Jersey,jaxb和jax-rs时设置xml命名空间
我们正在编写REST服务,并且当有人请求具有不存在的父ID的资源时,如何做有争议.
示例:您要求提供与公司关联的人员列表,因此您GET使用ID 1,但该公司ID不存在.
我认为REST的定义说我们会简单地返回一个空列表(导致a HTTP 204 (No Content)),因为根据规范,HTTP Bad Request仅用于格式错误的语法:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
由于语法格式错误,服务器无法理解请求.客户端不应该在没有修改的情况下重复请求.
我认为更清楚的是没有要解释的错误,您请求的资源不存在.
关于最佳实践的想法?
这里有一个SO讨论:HTTP 400(错误的请求)用于逻辑错误,而不是格式错误的请求语法,虽然它有点抽象,如果我发布这个,或者只是使用那个问题,我就会被撕裂.
我正在尝试获取在Tomcat中部署的Jersey资源的servlet上下文,但每当我尝试这个时,我得到一个空指针异常.我的代码如下:
@Path("/resource")
public class MyResource {
@Context ServletContext context;
public MyResource(){
System.out.println("Context: "+context.getRealPath("/"));
}
}
Run Code Online (Sandbox Code Playgroud)
我的web.xml配置如下:
<servlet>
<servlet-name>MyResource</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mypackage</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyResource</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
任何线索我的问题可能是什么?使用Tomcat 6.0.33和Jersey 1.12.
jax-rs ×10
java ×7
jersey ×7
jersey-2.0 ×2
rest ×2
servlets ×2
cdi ×1
http ×1
http-error ×1
interceptor ×1
jackson ×1
jakarta-ee ×1
java-ee-7 ×1
jaxb ×1
json ×1
tomcat ×1
web-services ×1