How Deserialize data time for response handler REST TEMPLATE? - java

I am using ResponseErrorHandler interface to handle the server error and client error
I get this response from an external API
{
"timestamp": "2021-08-02T12:04:45.332+0000",
"status": 400,
"error": "Bad Request",
"message": "Null Value Exception",
}
Now I have class where I am deserializing
public class ApplicationPayload {
#JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime timestamp;
private HttpStatus status;
private String error;
private String message;
//getter and setter
}
#Override
public void handleError(ClientHttpResponse response) throws IOException {}
I am getting this error in handleError method
Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2021-08-02T12:04:45.332+0000' could not be parsed, unparsed text found at index 23

Related

Spring Boot BindingResult returns 2 same error messages on one field

I tried using annotation #NotNull and #Past with my field for validation, but when I posted with null value in PostMan, it gave me 2 error responses instead of 1, is this a problem and if it is, how to fix it?
Error Handler:
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
final Map<String, String> errors = new HashMap<>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.put(error.getField(), String.format("%s %s", error.getField(), error.getDefaultMessage()));
}
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
The field that needs to validate:
#Past
#NotNull
private Date birthDate;
Request body:
{
"birthDate":""
}
Response:
[
{
"field": "birthDate",
"message": "must not be null"
},
{
"field": "birthDate",
"message": "must not be null"
}
]

Spring boot controller - Download Multipart and JSON to DTO

I need get a JAVA Object (DTO) with a MultipartFile in a Spring Controller
I tried different ways, like use of produces = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE} but nothing works.
The "DTO" to get is:
public class TodoDTO {
private Long id;
private String description;
private Boolean status;
private MultipartFile image;
...
}
And the controller method is:
#GetMapping(produces = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<List<TodoDTO>> getAll() {
ResponseEntity<List<TodoDTO>> response;
try {
response = new ResponseEntity<List<TodoDTO>>(todoService.getAll(), HttpStatus.FOUND);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e);
}
return response;
}
I will expect to get entire object, with Multipart and the other properties. But the response in Postman is:
"status": 406,
"error": "Not Acceptable",
"message": "Could not find acceptable representation",

How to handle Request with JSON and Multipart File in spring boot

I am trying to create a REST API in the format which will accept request data with both JSON and MultipartFile type.
THis is my request which will be in following format in POSTMAN:
My POJO classes are as follows:
Class:Organisation
public class Organisation
{
priavet int org_id;
private MultipartFile organisationRegistrationDocument;
private Teachers[]
// getters and setters
}
Class: Teachers
class Teachers{
private String teacherId;
private MultipartFile teacherPhoto;
// getters and setters
}
My controller Class is as follows:
#RequestMapping(value="/test",method=RequestMethod.POST,headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
private ResponseEntity<Object> testUpload(#RequestBody Organisation org) {
return null;
}
Error Thrown from POSTMAN:
{
"timestamp": "2018-10-03T07:38:30.439+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request part 'org' is not present",
"path": "/test"
}
So anyone can kindly guide me what can I am doing wrong due to which I am not able to achieve the desired result to process request of the above form?

Missing response body from RestClientResponseException in Spring REST response

In my Spring Boot REST application I have an endpoint, where I do some stuff. There I also have a #Provider, where I can catch and map all exceptions occurred during the process:
#Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
#Override
public Response toResponse(Throwable ex) {
ErrorObject error = new ErrorObject("INTERNAL", 500, ex.getMessage());
return Response.status(error.getStatus()).entity(error).type(MediaType.APPLICATION_JSON).build();
}
}
The ErrorObject is just a basic pojo with some information:
public class ErrorObject implements Serializable {
private static final long serialVersionUID = 4181809471936547469L;
public ErrorObject(String name, int status, String message) {
this.name = name;
this.status = status;
this.message = message;
}
private String name;
private int status;
private String message;
setters/getters
}
If I call my endpoint with postman, I got this response if the exception occurs, and this is perfect:
{
"name": "INTERNAL",
"status": 500,
"message": "something happened",
}
But when I call the endpoint within my application, I catch the RestClientResponseException (which is basically HttpClientErrorException), I can see in the exception that it was 500, but there is no body, it is empty.
This is how I call it within my application:
try {
ResponseEntity<WhateverObject> entity = restTemplate.exchange(url, HttpMethod.POST, getBaseHeadersAsHttpEntity(), WhateverObject.class);
//...
} catch (RestClientResponseException e) {
//... ErrorObject of exception is missing here
}
How can I get the same body in case of exception, so my own ErrorObject from the exception?
Thanks for the comment of #Hemant Patel, after trying to set a new ErrorHandler, I figured out that the only thing I need to set is a new request factory:
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
And this factory is able to set the response body in the background successfully.

How to add a sub-status (error code) to a REST response in Spring?

I'm using Spring in a RESTful application and I throw a few HTTP 401 exceptions when access is being denied.
Essentially, I'm simply throwing one or more exceptions annotated by:
#ResponseStatus(value = HttpStatus.UNAUTHORIZED)
Which returns something like this:
{
"timestamp": 1434805699008,
"status": 401,
"error": "Unauthorized",
"message": "[ERR:02-011] Access to '/user/account/{id}' is rejected!",
"path": "/user/account/{id}"
}
The problem is that there are many sub-reasons for throwing an HTTP 401, such as API-KEY is malformed, API-KEY is invalid (not hex, too short, too long), API-KEY has expired, etc.
I would like the client to be able to parse such sub-status. I would like to be able to receive something like this:
{
"timestamp": 1434805699008,
"status": 401,
"code": 12011,
"error": "Unauthorized",
"message": "[ERR:12-011] Access to '/user/account/{id}' is rejected!",
"path": "/user/account/{id}"
}
Where there would be a "code" property that could be consumed by the client. I do not want to resort to parsing string messages such as my "[ERR:02-011]" which is bad practice.
I tried extending all my exceptions with an AbstractWebException which has a code variable with proper getter and setter methods. But this does not seem to end up being serialized with the rest of the exception (not too sure why).
I want to keep it simple!
Thanks for any advice and any help.
Some code:
public abstract class AbstractWebException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int errorCode = 0;
public AbstractWebException(String message) {
super(message);
}
public AbstractWebException(int errorCode, String message) {
super(message);
setErrorCode(errorCode);
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}
... and...
/**
* <b>HTTP 401 - Unauthorized</b>
*/
#ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public class UnauthorizedException extends AbstractWebException {
private static final long serialVersionUID = 1L;
public UnauthorizedException(String message) {
super(message);
}
}

Categories

Resources