15
15
*/
16
16
package org .springframework .hateoas .mediatype .vnderrors ;
17
17
18
+ import lombok .EqualsAndHashCode ;
19
+ import lombok .Getter ;
20
+ import lombok .ToString ;
21
+
18
22
import java .util .ArrayList ;
19
23
import java .util .Arrays ;
24
+ import java .util .Collection ;
25
+ import java .util .Collections ;
20
26
import java .util .Iterator ;
21
27
import java .util .List ;
22
28
29
+ import org .springframework .hateoas .CollectionModel ;
23
30
import org .springframework .hateoas .Link ;
31
+ import org .springframework .hateoas .Links ;
24
32
import org .springframework .hateoas .RepresentationModel ;
25
- import org .springframework .lang . Nullable ;
33
+ import org .springframework .hateoas . server . core . Relation ;
26
34
import org .springframework .util .Assert ;
27
- import org .springframework .util .StringUtils ;
28
35
29
36
import com .fasterxml .jackson .annotation .JsonCreator ;
37
+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
38
+ import com .fasterxml .jackson .annotation .JsonInclude ;
30
39
import com .fasterxml .jackson .annotation .JsonProperty ;
31
- import com .fasterxml .jackson .annotation .JsonValue ;
40
+ import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
32
41
33
42
/**
34
43
* A representation model class to be rendered as specified for the media type {@code application/vnd.error}.
37
46
* @author Oliver Gierke
38
47
* @author Greg Turnquist
39
48
*/
40
- public class VndErrors implements Iterable <VndErrors .VndError > {
49
+ @ JsonPropertyOrder ({ "message" , "logref" , "total" , "_links" , "_embedded" })
50
+ @ JsonIgnoreProperties (ignoreUnknown = true )
51
+ @ EqualsAndHashCode
52
+ @ ToString
53
+ public class VndErrors extends CollectionModel <VndErrors .VndError > {
54
+
55
+ /**
56
+ * @deprecated Use {@link org.springframework.hateoas.IanaLinkRelations#HELP}
57
+ */
58
+ @ Deprecated public static final String REL_HELP = "help" ;
59
+
60
+ /**
61
+ * @deprecated Use {@link org.springframework.hateoas.IanaLinkRelations#DESCRIBES}
62
+ */
63
+ @ Deprecated public static final String REL_DESCRIBES = "describes" ;
64
+
65
+ /**
66
+ * @deprecated Use {@link org.springframework.hateoas.IanaLinkRelations#ABOUT}
67
+ */
68
+ @ Deprecated public static final String REL_ABOUT = "about" ;
69
+
70
+ private final List <VndError > errors ;
71
+
72
+ @ Getter //
73
+ @ JsonInclude (value = JsonInclude .Include .NON_EMPTY ) //
74
+ private final String message ;
75
+
76
+ @ Getter //
77
+ @ JsonInclude (value = JsonInclude .Include .NON_EMPTY ) //
78
+ private final Integer logref ;
41
79
42
- private final List <VndError > vndErrors ;
80
+ public VndErrors () {
81
+
82
+ this .errors = new ArrayList <>();
83
+ this .message = null ;
84
+ this .logref = null ;
85
+ }
43
86
44
87
/**
45
88
* Creates a new {@link VndErrors} instance containing a single {@link VndError} with the given logref, message and
46
89
* optional {@link Link}s.
47
- *
48
- * @param logref must not be {@literal null} or empty.
49
- * @param message must not be {@literal null} or empty.
50
- * @param links
51
90
*/
52
91
public VndErrors (String logref , String message , Link ... links ) {
53
- this (new VndError (logref , message , links ));
92
+ this (new VndError (message , null , Integer . parseInt ( logref ) , links ));
54
93
}
55
94
56
95
/**
@@ -62,9 +101,11 @@ public VndErrors(VndError error, VndError... errors) {
62
101
63
102
Assert .notNull (error , "Error must not be null" );
64
103
65
- this .vndErrors = new ArrayList <>(errors .length + 1 );
66
- this .vndErrors .add (error );
67
- this .vndErrors .addAll (Arrays .asList (errors ));
104
+ this .errors = new ArrayList <>();
105
+ this .errors .add (error );
106
+ Collections .addAll (this .errors , errors );
107
+ this .message = null ;
108
+ this .logref = null ;
68
109
}
69
110
70
111
/**
@@ -73,138 +114,134 @@ public VndErrors(VndError error, VndError... errors) {
73
114
* @param errors must not be {@literal null} or empty.
74
115
*/
75
116
@ JsonCreator
76
- public VndErrors (List <VndError > errors ) {
117
+ public VndErrors (@ JsonProperty ("_embedded" ) List <VndError > errors , @ JsonProperty ("message" ) String message ,
118
+ @ JsonProperty ("logref" ) Integer logref , @ JsonProperty ("_links" ) Links links ) {
119
+
120
+ Assert .notNull (errors , "Errors must not be null!" ); // Retain for compatibility
121
+ Assert .notEmpty (errors , "Errors must not be empty!" );
77
122
78
- Assert .notNull (errors , "Errors must not be null!" );
79
- Assert .isTrue (!errors .isEmpty (), "Errors must not be empty!" );
80
- this .vndErrors = errors ;
123
+ this .errors = errors ;
124
+ this .message = message ;
125
+ this .logref = logref ;
126
+
127
+ if (links != null && !links .isEmpty ()) {
128
+ add (links );
129
+ }
81
130
}
82
131
83
- /**
84
- * Protected default constructor to allow JAXB marshalling.
85
- */
86
- protected VndErrors () {
87
- this .vndErrors = new ArrayList <>();
132
+ public VndErrors withMessage (String message ) {
133
+ return new VndErrors (this .errors , message , this .logref , this .getLinks ());
88
134
}
89
135
90
- /**
91
- * Adds an additional {@link VndError} to the wrapper.
92
- *
93
- * @param error
94
- */
95
- public VndErrors add (VndError error ) {
96
- this .vndErrors .add (error );
97
- return this ;
136
+ public VndErrors withLogref (Integer logref ) {
137
+ return new VndErrors (this .errors , this .message , logref , this .getLinks ());
98
138
}
99
139
100
- /**
101
- * Dummy method to allow {@link JsonValue} to be configured.
102
- *
103
- * @return the vndErrors
104
- */
105
- @ JsonValue
106
- private List <VndError > getErrors () {
107
- return vndErrors ;
140
+ public VndErrors withErrors (List <VndError > errors ) {
141
+
142
+ Assert .notNull (errors , "errors must not be null!" );
143
+ Assert .notEmpty (errors , "errors must not empty!" );
144
+
145
+ return new VndErrors (errors , this .message , this .logref , this .getLinks ());
108
146
}
109
147
110
- /*
111
- * (non-Javadoc)
112
- * @see java.lang.Iterable#iterator()
148
+ public VndErrors withError (VndError error ) {
149
+
150
+ this .errors .add (error );
151
+ return new VndErrors (this .errors , this .message , this .logref , this .getLinks ());
152
+ }
153
+
154
+ public VndErrors withLink (Link link ) {
155
+
156
+ add (link );
157
+ return new VndErrors (this .errors , this .message , this .logref , this .getLinks ());
158
+ }
159
+
160
+ public VndErrors withLinks (Link ... links ) {
161
+
162
+ add (links );
163
+ return new VndErrors (this .errors , this .message , this .logref , this .getLinks ());
164
+ }
165
+
166
+ /**
167
+ * Returns the underlying elements.
168
+ *
169
+ * @return the content will never be {@literal null}.
113
170
*/
114
171
@ Override
115
- public Iterator < VndErrors . VndError > iterator () {
116
- return this .vndErrors . iterator () ;
172
+ public Collection < VndError > getContent () {
173
+ return this .errors ;
117
174
}
118
175
119
- /*
120
- * (non-Javadoc)
121
- * @see java.lang.Object#toString()
176
+ /**
177
+ * Virtual attribute to generate JSON field of {@literal total}. Only generated when there are multiple errors.
122
178
*/
123
- @ Override
124
- public String toString () {
125
- return String .format ("VndErrors[%s]" , StringUtils .collectionToCommaDelimitedString (vndErrors ));
179
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
180
+ public Integer getTotal () {
181
+ return this .errors .size () > 1 //
182
+ ? this .errors .size () //
183
+ : null ; //
126
184
}
127
185
128
- /*
129
- * (non-Javadoc)
130
- * @see java.lang.Object#hashCode()
186
+ /**
187
+ * Adds an additional {@link VndError} to the wrapper.
188
+ *
189
+ * @param error
131
190
*/
132
- @ Override
133
- public int hashCode () {
134
- return vndErrors .hashCode ();
191
+ public VndErrors add (VndError error ) {
192
+ return withError (error );
135
193
}
136
194
137
195
/*
138
196
* (non-Javadoc)
139
- * @see java.lang.Object#equals(java.lang.Object )
197
+ * @see java.lang.Iterable#iterator( )
140
198
*/
141
199
@ Override
142
- public boolean equals (@ Nullable Object obj ) {
143
-
144
- if (this == obj ) {
145
- return true ;
146
- }
147
-
148
- if (!(obj instanceof VndErrors )) {
149
- return false ;
150
- }
151
-
152
- VndErrors that = (VndErrors ) obj ;
153
- return this .vndErrors .equals (that .vndErrors );
200
+ public Iterator <VndErrors .VndError > iterator () {
201
+ return this .errors .iterator ();
154
202
}
155
203
156
204
/**
157
205
* A single {@link VndError}.
158
206
*
159
207
* @author Oliver Gierke
208
+ * @author Greg Turnquist
160
209
*/
210
+ @ JsonPropertyOrder ({ "message" , "path" , "logref" })
211
+ @ Relation (collectionRelation = "errors" )
212
+ @ EqualsAndHashCode
161
213
public static class VndError extends RepresentationModel <VndError > {
162
214
163
- @ JsonProperty private final String logref ;
164
- @ JsonProperty private final String message ;
215
+ @ Getter //
216
+ private final String message ;
217
+
218
+ @ Getter (onMethod = @ __ (@ JsonInclude (JsonInclude .Include .NON_EMPTY ))) //
219
+ private final String path ;
220
+
221
+ @ Getter (onMethod = @ __ (@ JsonInclude (JsonInclude .Include .NON_EMPTY ))) //
222
+ private final Integer logref ;
165
223
166
224
/**
167
- * Creates a new {@link VndError} with the given logref, a message as well as some {@link Link}s .
225
+ * Creates a new {@link VndError} with a message and optional a path and a logref .
168
226
*
169
227
* @param logref must not be {@literal null} or empty.
170
228
* @param message must not be {@literal null} or empty.
171
229
* @param links
172
230
*/
173
- public VndError (String logref , String message , Link ... links ) {
231
+ @ JsonCreator
232
+ public VndError (@ JsonProperty ("message" ) String message , @ JsonProperty ("path" ) String path ,
233
+ @ JsonProperty ("logref" ) Integer logref , @ JsonProperty ("_links" ) List <Link > links ) {
174
234
175
- Assert .hasText (logref , "Logref must not be null or empty!" );
176
235
Assert .hasText (message , "Message must not be null or empty!" );
177
236
178
- this .logref = logref ;
179
237
this .message = message ;
180
- this .add (Arrays .asList (links ));
181
- }
182
-
183
- /**
184
- * Protected default constructor to allow JAXB marshalling.
185
- */
186
- protected VndError () {
187
-
188
- this .logref = null ;
189
- this .message = null ;
190
- }
191
-
192
- /**
193
- * Returns the logref of the error.
194
- *
195
- * @return the logref
196
- */
197
- public String getLogref () {
198
- return logref ;
238
+ this .path = path ;
239
+ this .logref = logref ;
240
+ this .add (links );
199
241
}
200
242
201
- /**
202
- * Returns the message of the error.
203
- *
204
- * @return the message
205
- */
206
- public String getMessage () {
207
- return message ;
243
+ public VndError (String message , String path , Integer logref , Link ... link ) {
244
+ this (message , path , logref , Arrays .asList (link ));
208
245
}
209
246
210
247
/*
@@ -215,40 +252,5 @@ public String getMessage() {
215
252
public String toString () {
216
253
return String .format ("VndError[logref: %s, message: %s, links: [%s]]" , logref , message , getLinks ().toString ());
217
254
}
218
-
219
- /*
220
- * (non-Javadoc)
221
- * @see org.springframework.hateoas.ResourceSupport#hashCode()
222
- */
223
- @ Override
224
- public int hashCode () {
225
-
226
- int result = 17 ;
227
-
228
- result += 31 * logref .hashCode ();
229
- result += 31 * message .hashCode ();
230
-
231
- return result ;
232
- }
233
-
234
- /*
235
- * (non-Javadoc)
236
- * @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object)
237
- */
238
- @ Override
239
- public boolean equals (@ Nullable Object obj ) {
240
-
241
- if (obj == this ) {
242
- return true ;
243
- }
244
-
245
- if (!(obj instanceof VndError )) {
246
- return false ;
247
- }
248
-
249
- VndError that = (VndError ) obj ;
250
-
251
- return this .logref .equals (that .logref ) && this .message .equals (that .message );
252
- }
253
255
}
254
256
}
0 commit comments