如何确保null
请求有效载荷不会发送到任何POST
或PUT
请求?
You could try implementing a ContainerRequestFilter
and abort the request if the payload is null.
requestContext.abortWith(Response.status(Response.Status.BAD_REQUEST)
.entity("Payload is null")
.build());
https://jersey.java.net/documentation/latest/filters-and-interceptors.html
/*Name Binding meta annotation*/
package com.avinash.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.NameBinding;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding
public @interface NullPayloadDetection {}
/* Decorate your filter with name-binding (NullPayloadDetection) annotation*/
package com.avinash.api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@Provider
@NullPayloadDetection
public class NullPayloadDetectionFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String entityStream = read(requestContext.getEntityStream());
if( (!requestContext.hasEntity() && requestContext.getLength() == 0)
|| isEmpty(entityStream)) {
String path = requestContext.getUriInfo().getRequestUri().getPath();
System.out.println("Null payload detected for "+path);
requestContext.abortWith(Response.status(Response.Status.BAD_REQUEST).entity("Null payload").build());
} else {
System.out.println("Nothing wrong with the request");
InputStream targetStream = new ByteArrayInputStream(entityStream.getBytes());
requestContext.setEntityStream(targetStream);
}
}
private String read(InputStream inputStream) throws IOException {
ByteArrayOutputStream into = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int n; 0 < (n = inputStream.read(buf));) {
into.write(buf, 0, n);
}
into.close();
String str = new String(into.toByteArray(), "UTF-8");
System.out.println(str);
return str;
}
private boolean isEmpty(String str) {
boolean bool = "".equals(str);
System.out.println("isEmpty: "+bool);
return bool;
}
}
/*
name-binding annotation is applied to the resource method to which the name-bound JAX-RS provider(s) should be bound to
*/
package com.avinash.api;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("status")
public class Status {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NullPayloadDetection
public Response status(MyRequestModel request) {
// your business logic
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.