#RestController custom ResponseEntity - Jackson - json

What i'm trying to achieve here is to get a custom response from the RequestMapping, below is the structure of the json which I'm trying to get in case of an array of objects:
{
"error": false,
"message": "the message",
"data": [{},{},...]
}
and the below in case of object
{
"error": false,
"message": "the message",
"data": {}
}
The code is working fine but the problem is "data" will not always has an array, it may store an object, so what I tried is to create a custom POJO class which contains my custom response and when I want to annotate two attributes with same name i'm getting the below error
Could not find acceptable representation
And what if I create another class which will contain the same attributes but with an JsonObject not with array, is there any better way to achieve this ?
Below are my classes :
#JsonInclude(Include.NON_NULL)
public class JsonResponseObject<T> implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private boolean error;
private String message ;
#JsonProperty(value="data")
private ArrayList<T> array;
#JsonProperty(value="data")
private Object object ;
public JsonResponseObject() {
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ArrayList<T> getArray() {
return array;
}
public void setArray(ArrayList<T> array) {
this.array = array;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
UserJsonController.java :
#RestController()
#RequestMapping(value = "/json")
public class UserJsonController {
#Autowired
private UserRepository userDAO;
#RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getUsers() {
ArrayList<Users> entityList = (ArrayList<Users>) userDAO.findAll();
JsonResponseObject<Users> jsonResponse = new JsonResponseObject<Users>();
jsonResponse.setError(false);
jsonResponse.setMessage("test");
jsonResponse.setArray(entityList);
return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
}
#RequestMapping(value = "/users/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getUserByID(#PathVariable int id) {
JsonResponseObject<Users> jsonResponse = new JsonResponseObject<Users>();
jsonResponse.setError(false);
jsonResponse.setMessage("test");
jsonResponse.setObject(userDAO.findById(id).get());
return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
}}

Related

How to retrieve field value from json object when object name has DOT in it

I want to extract "msg" value from below json using fasterxml.jackson - Can anyone suggest me how my model class should look like?
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Bad data received",
"err_data": {
"payment_details.type": {
"location": "body",
"param": "payment_details.type",
"msg": "Must be either etransfer or cheque"
}
}
}
This is what I have done, but it is always returning "null" !
#JsonInclude(JsonInclude.Include.ALWAYS)
public class MyApiResponse extends ParentResponse implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("payment_details")
private PaymentDetails payment_details;
#JsonProperty("payment_details")
public PaymentDetails getPayment_details() {
return payment_details;
}
#JsonProperty("payment_details")
public void setPayment_details(PaymentDetails payment_details) {
this.payment_details = payment_details;
}
}
ParentResponse model class extends ErrorResponse model class and this is how it looks like..
This ErrorResponse model represents above mentioned JSON.
#JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("statusCode")
private int statusCode;
#JsonProperty("error")
private String error;
#JsonProperty("message")
private String message;
#JsonProperty("err_data")
private ErrData err_data;
#JsonProperty("statusCode")
public int getStatusCode() {
return statusCode;
}
#JsonProperty("statusCode")
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
#JsonProperty("message")
public String getMessage() {
return message;
}
#JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
#JsonProperty("error")
public String getError() {
return error;
}
#JsonProperty("error")
public void setError(String error) {
this.error = error;
}
#JsonProperty("err_data")
public ErrData getErr_data() {
return err_data;
}
#JsonProperty("err_data")
public void setErr_data(ErrData err_data) {
this.err_data = err_data;
}
}
err_data object is represented by below model class.
#JsonInclude(JsonInclude.Include.ALWAYS)
public class ErrData implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("email")
private Email email;
#JsonProperty("payment_details.type")
private PaymentDetailsType payment_details_type;
#JsonProperty("email")
public Email getEmail() {
return email;
}
#JsonProperty("email")
public void setEmail(Email email) {
this.email = email;
}
#JsonProperty("payment_details.type")
public PaymentDetailsType getPayment_details_type() {
return payment_details_type;
}
#JsonProperty("payment_details.type")
public void setPayment_details_type(PaymentDetailsType payment_details_type) {
this.payment_details_type = payment_details_type;
}
}
payment_details.type object represented by below class.
#JsonInclude(JsonInclude.Include.ALWAYS)
public class PaymentDetailsType extends ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
}
#JsonInclude(JsonInclude.Include.ALWAYS)
public class Email extends ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
}
And finally ErrorMessage which is extended by PaymentDetailsType as below.
#JsonPropertyOrder({"location", "param", "value", "msg"})
public class ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("location")
private String location;
#JsonProperty("param")
private String param;
#JsonProperty("value")
private String value;
#JsonProperty("msg")
private String msg;
#JsonProperty("location")
public String getLocation() {
return location;
}
#JsonProperty("location")
public void setLocation(String location) {
this.location = location;
}
#JsonProperty("param")
public String getParam() {
return param;
}
#JsonProperty("param")
public void setParam(String param) {
this.param = param;
}
#JsonProperty("value")
public String getValue() {
return value;
}
#JsonProperty("value")
public void setValue(String value) {
this.value = value;
}
#JsonProperty("msg")
public String getMsg() {
return msg;
}
#JsonProperty("msg")
public void setMsg(String msg) {
this.msg = msg;
}
}
And finally I am trying to get "msg" field value as below -
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getPayment_details_type().getMsg();
I think there is something wrong with this one - Not sure how to define getter method if field name in json as . (dot).
#JsonProperty("payment_details.type")
public PaymentDetailsType getPayment_details_type() {
return payment_details_type;
}
Similar to above, I am doing it for below json to retrieve "msg" value and it is working fine.
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Bad data received",
"err_data": {
"email": {
"location": "body",
"param": "email",
"value": "test # com",
"msg": "Must be a valid email"
}
}
}
This is returning correct "msg" value.
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getEmail().getMsg();
Please suggest!
Thank you.
Here is a minimal example showing how to parse a JSON with Jackson, where the property names may contain dots:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
class Main {
public static void main(String[] args) throws IOException {
String json = "{" +
" \"payment_details.type\": {" +
" \"location\": \"body\"" +
" }" +
"}";
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(json, Response.class);
System.out.println(response.getPaymentDetailsType().getLocation());
}
}
class Response {
#JsonProperty("payment_details.type")
private PaymentDetailsType paymentDetailsType;
public PaymentDetailsType getPaymentDetailsType() { return paymentDetailsType; }
}
class PaymentDetailsType {
private String location;
public String getLocation() { return location; }
}
Note that you only need #JsonProperty when the expected property name in JSON cannot be deduced from a setter or variable name
Its also working with #JsonProperty.
You just have to escape the dot:
#JsonProperty("payment_details\\.type")

How to extract particular field value from the json api response using fasterxml.jackson

One of my api response is as below -
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Bad data received",
"err_data": {
"email": {
"location": "body",
"param": "email",
"value": false,
"msg": "Please provide valid e-mail address"
}
}
}
So, in below response.asString() represents above response body.
ApiResponse response = new Gson().fromJson(response.asString(), ApiResponse.class);
ApiResponse.class is my model which is as below:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "statusCode", "message" })
public class ApiResponse implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("message")
private String message;
#JsonProperty("statusCode")
private int statusCode;
#JsonProperty("err_data")
private List<String> errData = new ArrayList<>();
#JsonProperty("email")
private List<String> email = new ArrayList<>();
#JsonProperty("msg")
private String msg;
/**
* No args constructor for use in serialization
*/
public ApiResponse() {
}
/**
* #param message
*/
public ApiResponse(int statusCode, String message, List<String> errData, List<String> email, String msg) {
this.message = message;
this.statusCode = statusCode;
this.errData = errData;
this.email = email;
this.msg = msg;
}
#JsonProperty("statusCode")
public int getStatusCode() {
return statusCode;
}
#JsonProperty("statusCode")
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
#JsonProperty("message")
public String getMessage() {
return message;
}
#JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
#JsonProperty("err_data")
public List<String> getErrData() {
return errData;
}
#JsonProperty("err_data")
public void setErrData(List<String> errData) {
this.errData = errData;
}
#JsonProperty("email")
public List<String> getEmail() {
return email;
}
#JsonProperty("email")
public void setEmail(List<String> email) {
this.email = email;
}
#JsonProperty("msg")
public String getMsg() {
return msg;
}
#JsonProperty("msg")
public void setMsg(String msg) {
this.msg = msg;
}
}
When I am trying to get msg under "email":{}, it is returning null.
ApiResponse apiResponse = new Gson().fromJson(response.asString(), ApiResponse.class);
// this prints correct value
System.out.println(apiResponse.getMessage());
// this prints correct value
System.out.println(apiResponse.getStatusCode());
// this prints empty string array => []
System.out.println(apiResponse.getErrData());
// this also prints empty string array => []
System.out.println(apiResponse.getEmail());
// this prints null
System.out.println(apiResponse.getMsg());
I am new to fasterxml.jackson lib and not sure what I am missing.
To get msg value, what changes I'll have to do in model class above. Thank you very much in advance.
This is where your code is incorrect :
#JsonProperty("err_data")
private List<String> errData = new ArrayList<>();
#JsonProperty("email")
private List<String> email = new ArrayList<>();
Both email and errData is not a List, they are a separate Object. Just like the ApiResponse.java, you need to create POJO for both objects. For example :
public class Email {
private String location;
private String param;
private String value;
private String msg;
// define getter and setter
}
and
public class ErrData {
private Email email;
// define getter and setter
}
Then use the new class as the object type.
#JsonProperty("err_data")
private ErrData errData;
// email is already inside ErrData, you don't need to define them here
Finally to access your msg :
errData.getEmail().getMsg();
Hope this is clear enough. Good luck!

How do I customize default error message from spring #Valid validation?

DTO:
public class User {
#NotNull
private String name;
#NotNull
private String password;
//..
}
Controller:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<String> saveUser(#Valid #RequestBody User user) {
//..
return new ResponseEntity<>(HttpStatus.OK);
}
Default json error:
{"timestamp":1417379464584,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MethodArgumentNotValidException","message":"Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity<demo.User> demo.UserController.saveUser(demo.User), with 2 error(s): [Field error in object 'user' on field 'name': rejected value [null]; codes [NotNull.user.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.name,name]; arguments []; default message [name]]; default message [may not be null]],"path":"/user"}
I would like to have my custom json for each error occured. How do I accomplish that?
If you want full control over the response message in every controller write a ControllerAdvice. For example, that example transform MethodArgumentNotValidException into a custom json object:
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
/**
* Kudos http://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-adding-validation-to-a-rest-api/
*
*/
#Order(Ordered.HIGHEST_PRECEDENCE)
#ControllerAdvice
public class MethodArgumentNotValidExceptionHandler {
#ResponseStatus(BAD_REQUEST)
#ResponseBody
#ExceptionHandler(MethodArgumentNotValidException.class)
public Error methodArgumentNotValidException(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<org.springframework.validation.FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
private Error processFieldErrors(List<org.springframework.validation.FieldError> fieldErrors) {
Error error = new Error(BAD_REQUEST.value(), "validation error");
for (org.springframework.validation.FieldError fieldError: fieldErrors) {
error.addFieldError(fieldError.getField(), fieldError.getDefaultMessage());
}
return error;
}
static class Error {
private final int status;
private final String message;
private List<FieldError> fieldErrors = new ArrayList<>();
Error(int status, String message) {
this.status = status;
this.message = message;
}
public int getStatus() {
return status;
}
public String getMessage() {
return message;
}
public void addFieldError(String path, String message) {
FieldError error = new FieldError(path, message);
fieldErrors.add(error);
}
public List<FieldError> getFieldErrors() {
return fieldErrors;
}
}
}
You can perform validation with Errors/BindingResult object.
Add Errors argument to your controller method and customize the error message when errors found.
Below is the sample example, errors.hasErrors() returns true when validation is failed.
#RequestMapping(value = "/user", method = RequestMethod.POST)
#ResponseBody
public ResponseEntity<String> saveUser(#Valid #RequestBody User user, Errors errors) {
if (errors.hasErrors()) {
return new ResponseEntity(new ApiErrors(errors), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK);
}
I know this is kind of old question,
But I just run into it and I found some pretty good article which has also a perfect example in github.
Basically it uses #ControllerAdvice as Spring documentation suggests.
So for example catching 400 error will be achieved by overriding one function:
#ControllerAdvice
public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final List<String> errors = new ArrayList<String>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() + ": " + error.getDefaultMessage());
}
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
}
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
}
(ApiError class is a simple object to hold status, message, errors)
One way to do it is adding message in #NotNull annotation on entity properties. And adding #Valid annotation in controller request body.
DTO:
public class User {
#NotNull(message = "User name cannot be empty")
private String name;
#NotNull(message = "Password cannot be empty")
private String password;
//..
}
Controller:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<String> saveUser(#Valid #RequestBody User user) {
//..
return new ResponseEntity<>(HttpStatus.OK);
}
// Add one
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<List<YourErrorResponse>> handleException(MethodArgumentNotValidException ex) {
// Loop through FieldErrors in ex.getBindingResult();
// return *YourErrorReponse* filled using *fieldErrors*
}
#ControllerAdvice(annotations = RestController.class)
public class GlobalExceptionHandler implements ApplicationContextAware {
#ExceptionHandler(MethodArgumentNotValidException.class)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public ApplicationError validationException(MethodArgumentNotValidException e) {
e.printStackTrace();
return new ApplicationError(SysMessageEnum.MSG_005, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}
}
You can do something like this
#ExceptionHandler(value = MethodArgumentNotValidException.class)
protected ResponseEntity<Error> handleGlobalExceptions(MethodArgumentNotValidException ex,
WebRequest request) {
log.catching(ex);
return new ResponseEntity<>(createErrorResp(HttpStatus.BAD_REQUEST,
ex.getBindingResult().getFieldErrors().stream().map(err -> err.getDefaultMessage())
.collect(java.util.stream.Collectors.joining(", "))),
HttpStatus.BAD_REQUEST);
}
For customized the error message in JSON format then do the below steps.
- Create one #Component called CommonErrorHandler
#Component
public class CommonErrorHandler {
public Map<String,Object> getFieldErrorResponse(BindingResult result){
Map<String, Object> fielderror = new HashMap<>();
List<FieldError>errors= result.getFieldErrors();
for (FieldError error : errors) {
fielderror.put(error.getField(), error.getDefaultMessage());
}return fielderror;
}
public ResponseEntity<Object> fieldErrorResponse(String message,Object fieldError){
Map<String, Object> map = new HashMap<>();
map.put("isSuccess", false);
map.put("data", null);
map.put("status", HttpStatus.BAD_REQUEST);
map.put("message", message);
map.put("timeStamp", DateUtils.getSysDate());
map.put("filedError", fieldError);
return new ResponseEntity<Object>(map,HttpStatus.BAD_REQUEST);
}
}
-- Add InvalidException class
public class InvalidDataException extends RuntimeException {
/**
* #author Ashok Parmar
*/
private static final long serialVersionUID = -4164793146536667139L;
private BindingResult result;
public InvalidDataException(BindingResult result) {
super();
this.setResult(result);
}
public BindingResult getResult() {
return result;
}
public void setResult(BindingResult result) {
this.result = result;
}
}
- Introduce #ControllerAdvice class
#ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler(InvalidDataException.class)
public ResponseEntity<?> invalidDataException(InvalidDataException ex, WebRequest request) {
List<FieldError> errors = ex.getResult().getFieldErrors();
for (FieldError error : errors) {
logger.error("Filed Name ::: " + error.getField() + "Error Message :::" + error.getDefaultMessage());
}
return commonErrorHandler.fieldErrorResponse("Error", commonErrorHandler.getFieldErrorResponse(ex.getResult()));
}
}
-- Use in controller with #Valid and throw exception
public AnyBeans update(**#Valid** #RequestBody AnyBeans anyBeans ,
BindingResult result) {
AnyBeans resultStr = null;
if (result.hasErrors()) {
**throw new InvalidDataException(result);**
} else {
resultStr = anyBeansService.(anyBeans );
return resultStr;
}
}
-- Output will be in JSON format
{
"timeStamp": 1590500231932,
"data": null,
"message": "Error",
"isSuccess": false,
"status": "BAD_REQUEST",
"filedError": {
"name": "Name is mandatory"
}
}
Hope this will be work. :-D
#ControllerAdvice
#RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// ex.getBindingResult(): extract the bind result for default message.
String errorResult = ex.getBindingResult().toString();
CustomizedExceptionHandlerResponse exceptionResponse = new CustomizedExceptionHandlerResponse(
errorResult, new Date(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
}
}
class CustomizedExceptionHandlerResponse {
private String message;
private String status;
private Date timestamp;
// constuctor, setters, getters...
}
you can use this code to iterate through errors and build a custom error message :
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.stream.Collectors;
#ControllerAdvice
public class ExceptionHandlerController {
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorDto> handleException(MethodArgumentNotValidException ex) {
ErrorDto dto = new ErrorDto(HttpStatus.BAD_REQUEST, "Validation error");
dto.setDetailedMessages(ex.getBindingResult().getAllErrors().stream()
.map(err -> err.unwrap(ConstraintViolation.class))
.map(err -> String.format("'%s' %s", err.getPropertyPath(), err.getMessage()))
.collect(Collectors.toList()));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(dto);
}
#Data
public static class ErrorDto {
private final int status;
private final String error;
private final String message;
private List<String> detailedMessages;
public ErrorDto(HttpStatus httpStatus, String message) {
status = httpStatus.value();
error = httpStatus.getReasonPhrase();
this.message = message;
}
}
}
This will give you a response like this in case of error :
{
"status": 400,
"error": "Bad Request",
"message": "Validation error",
"detailedMessages": [
"'yourField' should not be empty."
]
}
Add some information too.
If you use just #Valid, you need to catch BindException. If you use #Valid #RequestBody catch MethodArgumentNotValidException
Some sources:
HandlerMethodArgumentResolverComposite.getArgumentResolver(MethodParameter parameter):129 - search which HandlerMethodArgumentResolver support such parameter
RequestResponseBodyMethodProcessor.supportsParameter(MethodParameter parameter) - return true if parameter has annotation #RequestBody
RequestResponseBodyMethodProcessor:139 - throw MethodArgumentNotValidException
ModelAttributeMethodProcessor:164 - throw BindException
I know this is an old question, but i stumbled across and decided to show a cleaner way of doing what Ksokol showed.
StandardError class:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class StandardError {
private Instant timestamp;
private Integer status;
private String error;
private String message;
private String path;
}
ControllerExceptionHandler class:
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<StandardError> notValid(MethodArgumentNotValidException e, HttpServletRequest request) {
String error = "Dados inválidos.";
HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
StandardError err = new StandardError(
Instant.now(),
status.value(),
error,
e.getBindingResult()
.getFieldErrors()
.stream()
.map(FieldError::getDefaultMessage)
.collect(Collectors.toSet())
.toString()
.replaceAll("\\[*]*", ""),
request.getRequestURI()
);
return ResponseEntity.status(status).body(err);
}
This way if you have a custom message on your bean validator, it will appear formatted in a error.
Result:

GSON throwing “Expected BEGIN_OBJECT but was STRING

This is Json Object
[
{
"UserId":"demouser1",
"Catagories":[
{
"CatagoryName":"Entertainment",
"Persent":"25"
},
{
"CatagoryName":"Household",
"Persent":"25"
},
{
"CatagoryName":"Movie",
"Persent":"25"
},
{
"CatagoryName":"Misc",
"Persent":"25"
}
],
"RequestId":null,
"ResponseStatus":false,
"Token":null
}
]
Used The Following approach to parse the above Json
public class CategoryEntity {
private String CatagoryName;
private String Persent;
public String getCatagoryName() {
return CatagoryName;
}
public void setCatagoryName(String catagoryName) {
CatagoryName = catagoryName;
}
public String getPersent() {
return Persent;
}
public void setPersent(String persent) {
Persent = persent;
}
}
import java.util.List;
public class Entity {
private String UserId;
public String getUserId() {
return UserId;
}
public void setUserId(String userId) {
UserId = userId;
}
public List<CategoryEntity> getListCatagories() {
return ListCatagories;
}
public void setListCatagories(List<CategoryEntity> listPMMCatagories) {
ListCatagories = listPMMCatagories;
}
public String getRequestId() {
return RequestId;
}
public void setRequestId(String requestId) {
RequestId = requestId;
}
public boolean isResponseStatus() {
return ResponseStatus;
}
public void setResponseStatus(boolean responseStatus) {
ResponseStatus = responseStatus;
}
private List<CategoryEntity> ListCatagories;
private String RequestId;
private String Token;
public String getToken() {
return Token;
}
public void setToken(String token) {
Token = token;
}
private boolean ResponseStatus;
}
And
Following approach to convert the json object to corresponding object
Gson gson =new Gson();
JsonPrimitive listCatagoriesElement= element.getAsJsonPrimitive();
System.out.println("listCatagoriesElement.getAsString()>>"+listCatagoriesElement.getAsString());
sysout prints: listCatagoriesElement.getAsString()>>[{"UserId":"user1","ListCatagories":[{"CatagoryName":"Entertainment","Persent":"25"},{"CatagoryName":"Household","Persent":"25"},{"CatagoryName":"Movie","Persent":"25"},{"CatagoryName":"Misc","Persent":"25"}],"RequestId":null,"ResponseStatus":false,"Token":null}]
Entity entity = gson.fromJson(listCatagoriesElement, Entity.class);
Any ideas how should I fix it?
Thanks!
Your class CategoryEntity is correct, but in your class Entity, the attribute ListCatagories should be called Catagories to match the name in the JSON!
Apart from that, in order to parse the JSON you'd better do something like this:
Gson gson = new Gson();
Type listType = new TypeToken<List<Entity>>() {}.getType();
List<Entity> entities = gson.fromJson(yourJsonString, listType);
So you'll have a List containing just one Entity object, and you can access the values just with:
String catagoryNameI = entities.get(0).getCatagories().get(i).getCatagoryName();
String persentI = entities.get(0).getCatagories().get(i).getPersent();
You have to do this because your whole JSON response is an array, surrounded by [ ... ], so you need to parse it into some List...

Simple way to strip outer array of responses in gson

I'm working with an api (Phillips Hue) that wraps all of it's json responses in an array with one entry (the content).
Example:
[{
"error": {
"type": 5,
"address": "/",
"description": "invalid/missing parameters in body"
}
}]
I usually write standard POJO's parsed by GSON to handle responses but since the response is not a json object I'm a bit stumped on the best way to deal with this. I didn't really want every object to actually be an array that I have to call .get(0) on.
Example of the POJO if it was a JSON obj and NOT wrapped in an array.
public class DeviceUserResponse {
private DeviceUser success;
private Error error;
public DeviceUser getSuccess() {
return success;
}
public Error getError() {
return error;
}
public static class Error {
private int type;
private String address;
private String description;
public int getType() {
return type;
}
public String getAddress() {
return address;
}
public String getDescription() {
return description;
}
#Override
public String toString() {
return "Type: " + this.type
+ " Address: " + this.address
+ " Description: " + this.description;
}
}
}
What I have to do right now:
ArrayList<DeviceUserResponse> response.get(0).getError();
Is there a way that I can strip this array for every response or am I just going to have to do a .get(0) in my POJO's and just not expose it?
I think you've to go with custom deserialization in order to "strip out" the array.
Here a possible solution.
An adapter for your response POJO:
public class DeviceUserResponseAdapter extends TypeAdapter<DeviceUserResponse> {
protected TypeAdapter<DeviceUserResponse> defaultAdapter;
public DeviceUserResponseAdapter(TypeAdapter<DeviceUserResponse> defaultAdapter) {
this.defaultAdapter = defaultAdapter;
}
#Override
public void write(JsonWriter out, DeviceUserResponse value) throws IOException {
defaultAdapter.write(out, value);
}
#Override
public DeviceUserResponse read(JsonReader in) throws IOException {
in.beginArray();
assert(in.hasNext());
DeviceUserResponse response = defaultAdapter.read(in);
in.endArray();
return response;
}
}
A factory for your adapter:
public class DeviceUserResponseAdapterFactory implements TypeAdapterFactory {
#Override
#SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType()!=DeviceUserResponse.class) return null;
TypeAdapter<DeviceUserResponse> defaultAdapter = (TypeAdapter<DeviceUserResponse>) gson.getDelegateAdapter(this, type);
return (TypeAdapter<T>) new DeviceUserResponseAdapter(defaultAdapter);
}
}
Then you've to register and user it:
DeviceUserResponseAdapterFactory adapterFactory = new DeviceUserResponseAdapterFactory();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.registerTypeAdapterFactory(adapterFactory).create();
DeviceUserResponse response = gson.fromJson(json, DeviceUserResponse.class);
System.out.println(response.getError());
This solution will not work if you have the DeviceUserResponse inside other complex JSON object. I that case the adapter will try to find an array and will terminate with an error.
Another solution is to parse it as array and then in your "communication" layer you get only the first element. This will preserve the GSon deserialization.
In the comment you're asking for a more generic solution, here one:
The adapter:
public class ResponseAdapter<T> extends TypeAdapter<T> {
protected TypeAdapter<T> defaultAdapter;
public ResponseAdapter(TypeAdapter<T> defaultAdapter) {
this.defaultAdapter = defaultAdapter;
}
#Override
public void write(JsonWriter out, T value) throws IOException {
defaultAdapter.write(out, value);
}
#Override
public T read(JsonReader in) throws IOException {
in.beginArray();
assert(in.hasNext());
T response = defaultAdapter.read(in);
in.endArray();
return response;
}
}
The factory:
public class ResponseAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if ((type.getRawType().getSuperclass() != Response.class)) return null;
TypeAdapter<T> defaultAdapter = (TypeAdapter<T>) gson.getDelegateAdapter(this, type);
return (TypeAdapter<T>) new ResponseAdapter<T>(defaultAdapter);
}
}
Where Response.class is your super class from which all the service responses inherit.
The first solution advices are still valid.