修订版 | 152 (tree) |
---|---|
时间 | 2018-03-14 19:45:55 |
作者 | hirukawa_ryo |
* jersey-log-viewer 0.2.1
jersey標準では戻り値として null を返すと 204 No Content になってしまいます。これを 200 OK で返し、ボディに null という4文字が設定されるようにしました。
@@ -15,12 +15,22 @@ | ||
15 | 15 | import java.util.regex.Matcher; |
16 | 16 | import java.util.regex.Pattern; |
17 | 17 | |
18 | +import javax.ws.rs.Produces; | |
18 | 19 | import javax.ws.rs.container.ContainerRequestContext; |
19 | 20 | import javax.ws.rs.container.ResourceInfo; |
21 | +import javax.ws.rs.core.MediaType; | |
20 | 22 | |
23 | +import com.fasterxml.jackson.core.JsonGenerator; | |
24 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
25 | +import com.fasterxml.jackson.databind.JsonSerializer; | |
26 | +import com.fasterxml.jackson.databind.SerializerProvider; | |
27 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
28 | + | |
21 | 29 | public class InvocationLogger implements InvocationHandler { |
22 | 30 | private static final Pattern SIMPLE_NAME = Pattern.compile("[^.<@]+\\."); |
23 | - | |
31 | + private static final NullObject NULL = new NullObject(); | |
32 | + | |
33 | + | |
24 | 34 | @Override |
25 | 35 | public Object invoke(Object obj, Method method, Object[] args) throws Throwable { |
26 | 36 | try { |
@@ -30,6 +40,15 @@ | ||
30 | 40 | } |
31 | 41 | try { |
32 | 42 | Object result = method.invoke(obj, args); |
43 | + if(result == null) { | |
44 | + try { | |
45 | + if(isProduceMediaTypeJson(method)) { | |
46 | + result = NULL; | |
47 | + } | |
48 | + } catch(Exception e) { | |
49 | + // ignore | |
50 | + } | |
51 | + } | |
33 | 52 | return result; |
34 | 53 | } catch(Throwable t) { |
35 | 54 | try { |
@@ -41,6 +60,24 @@ | ||
41 | 60 | } |
42 | 61 | } |
43 | 62 | |
63 | + private static boolean isProduceMediaTypeJson(Method method) { | |
64 | + Annotation[] annotations = method.getAnnotations(); | |
65 | + if(annotations != null) { | |
66 | + for(int i = 0; i < annotations.length; i++) { | |
67 | + Annotation annotation = annotations[i]; | |
68 | + if(annotation instanceof Produces) { | |
69 | + Produces produces = (Produces)annotation; | |
70 | + for(String s : produces.value()) { | |
71 | + if(MediaType.APPLICATION_JSON.equals(s)) { | |
72 | + return true; | |
73 | + } | |
74 | + } | |
75 | + } | |
76 | + } | |
77 | + } | |
78 | + return false; | |
79 | + } | |
80 | + | |
44 | 81 | private void log(Object obj, Object[] args) throws IOException { |
45 | 82 | ContainerRequestContext requestContext = getRequestContext(obj); |
46 | 83 | if(requestContext == null) { |
@@ -268,4 +305,15 @@ | ||
268 | 305 | m.appendTail(sb); |
269 | 306 | return sb.toString(); |
270 | 307 | } |
308 | + | |
309 | + @JsonSerialize(using=NullObjectSerializer.class) | |
310 | + public static class NullObject { | |
311 | + } | |
312 | + | |
313 | + public static class NullObjectSerializer extends JsonSerializer<NullObject> { | |
314 | + @Override | |
315 | + public void serialize(NullObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | |
316 | + gen.writeNull(); | |
317 | + } | |
318 | + } | |
271 | 319 | } |