Puspender Tanwar

Ranch Hand
+ Follow
since Apr 21, 2015
Puspender likes ...
Java
Cows and Likes
Cows
Total received
2
In last 30 days
0
Total given
0
Likes
Total received
32
Received in last 30 days
0
Total given
30
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Puspender Tanwar

Tim Holloway wrote:connects, submits data, gets a single response returned as quickly as possible, then closes the connection until it makes the next request (if any). That's a basic characteristic of the Internet and not a Java or Spring - or cloud - limitation.


Doesn't the reactive programming solve this? In reactive model, a thread is assigned to a request and if that takes some time, that thread can be used by some other connection request and once the response is ready that is sent back using some thread.

Why I am concerned? I read it somewhere that we shouldn't mix reactive and blocking architecture.
3 months ago
From the docs  https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-starter

Spring Cloud Gateway is built on Spring Boot 2.x, Spring WebFlux, and Project Reactor. As a consequence, many of the familiar synchronous libraries (Spring Data and Spring Security, for example) and patterns you know may not apply when you use Spring Cloud Gateway



I have two services which are blocking/synchronous in nature, but the Spring Cloud Gateway is on non-blocking model. So, should I use Spring Cloud Gateway as an API gateway?
3 months ago
But how to get the actual error JSON then? I need to parse the JSON body.
6 months ago
I am hitting a REST api using Spring Boot RestTemplate. If the error response is smaller, exception.getMessage() returns the complete JSON structure of error, but if the error JSON gets 3 4 more lines added, it wiped out from the few keys and add ...(557 bytes) at the last.

Getting perfect JSON error response if error body is smaller:


But if a few more keys gets added to the error JSON, I get the below string on `exception.getMessage()`


The same API if I hit using Postman this is the complete error JSON:


How can I get the complete JSON out of exception message? And first of all, why is there a limit?
6 months ago
I created a fat jar using Spring boot. Inside the bootstrap.yml I am trying to access a file which is under /resources only.
location: classpath:secret.json

But I am getting java.io.FileNotFoundException: class path resource [secret.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/test/Downloads/Demo/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/secret.json

The same is working in IDE. What could be the wrong?

Here is the decompiled jar file structure screenshot and package structure

8 months ago
Adding an answer for future users.
Yes you can as many fields as you want into your response. There are 2 situations for returning a response for a request:
1. The client wants the exact similar data as the DB table has, or 2 3 extra fields.
2. There need to a lot of extra fields and their are some additional fields which require some calculation or some other entity data. Like Post can have a PostType json field.

Solutions for 1:
Simply return the fetched entity. If you want to hide some field (e.g id), use @JsonIgnore on it.
If you want 2 3 additional fields, create those fields into your Entity class and mark them as @Transient. @Transient mark that field to not be included in SQLs generated by ORM

Solution for 2:
Use Dtos(Data Transfer Object). Add as many field as you want to them. Then either use setters to feed data into that DTO or use some library like MapStruct to automap data from Entity to DTO.

And that's it, you have your own flavor of response.

Now if you even wants to take advantage of HATEOAS, you can easily do that with your DTO.
9 months ago
I have below JPA Entity Privelege



But when I have an instance of Set<Privilege> on which I want to check if an PrivilegeId exists, I always gets false


In database I have 3 Privileges:
1 CAN_DELETE
2 CAN_ADD
3 CAN_EDIT

Of course, I can do this in another way by iterating over the Set<> and checking the value, but I wanted to try this way as well.

What I am doing wrong? Are equals() and hashcode() correct here?
9 months ago
I am using @CreationTimestamp for the createdAt column into my table. Now I want to compare the dates using LocalDateTime. The @CreationTimestamp stores the date in format like : "2020-11-20 17:36:46", but the LocalDatTime.now() gives "2020-11-20T17:36:46"




As of now, I am replacing the "T" in "2020-11-20T17:36:46" with space and then comparing it using JPQL query.
Is there any other way to solve this? Replacing "T" with space seems a hack to me.
1 year ago
I have build a REST api using Spring boot. The database has 39 tables and the number of Rest controllers are 31. Considering 2000 daily users which hit the API, how do I caclulate the RAM required on AWS ec2(or any xyz cloud provider) for running the API efficiently? As of now I am using their free tier which comes with 1core 1GB ram. But my application keeps on crashing. It stops even when I am not hitting the API. Sometimes logs are getting generated which shows memory issues and maximum times it stops without any server logs.
I have deployed the REST API using java -jar

Thanks
1 year ago
So I tested posting the new entity at 2020-05-27 14:15:20 UTC:

Output:
Before persist: null
getter: 2020-05-27T19:45:20


Means, it is done by the Hibernate using the JVM's default timezone.
1 year ago

Problems may occur if you allow the client to set fields of type LocalDate, LocalTime and LocalDateTime. In general it's not a good idea to use the same class to both represent data in your database and data received from the client. For instance, what if I POST your JSON example from Germany? First of all, why can the client POST commentId, createdAt and updatedAt at all? Those fields should be determined by the server, not the client


I have separate IN and OUT Dtos which uses mapping library MapStruct to map the IN and OUT fields. The createdAt and updatedAt are not sent by the client, those are set by the server. I removed all these details to avoid verbosity. I kept the bare minimum code.

You can find out which of the two does it by printing the value of comment.updatedAt before you save it in the repository and after you retrieve it from the repository

Yes I will check this out and post the results here.

Thanks for your time Stephan
1 year ago
This is the Comment entity:


POST operation:


GET operation : Repository, controller and service methods:


This is all I am doing apart from setting spring.jpa.properties.hibernate.jdbc.time_zone = UTC into the application.properties file. I have done nothing manually to change or manipulate the timezones.

So, I commented on 2020-05-02 06:49:53 and the createdAt time got saved into DB was 2020-05-02 01:19:53. Then I retrieved the comment and the createdAt recieved was 2020-05-02T06:49:53 which is rightly converted IST timezone which is UTC+5:30


{
 "commentId": 110,
 "content": "comment at 6:49:55",
 "createdAt": "2020-05-02T06:49:53",
 "updatedAt": "2020-05-02T06:49:53",
}
1 year ago
I have spring.jpa.properties.hibernate.jdbc.time_zone = UTC set into my project's application.properties file and it's also working fine. I am into IST timezond and when I POST data to my REST API, the UTC gets saved into the database. And when I fetch the data from my REST API, I can see the UTC being converted to `IST` automatically.

How does this happen internally? Does anything depend on the system's JVM timezone? What would happen when I deploy my REST API and the Database to AWS instance?
I am worried if this would work if I go into production. As of now, everything is local to my system.

Guidance, please.
1 year ago
Problem solved using javax.validation.Validator.validate(object) which gives the failed constraints.
1 year ago