Spring Data REST event handler send JSON response - java

I've been working on a Spring (backend) and ExtJS (UI) application. I am using Spring Data REST to reduce code, with just an interface am able to do CRUD on my web endpoints. The hard nut is getting my event handlers to send JSON response back to my client after an event such as before and after crud. My handlers are registered and working since events are being logged on the console. Now I need to send such a response if create bank is successful
{success:true, 'msg':'Bank created successfully '
Help?

If you take a look at fundamentals, your code would depend on the http response status codes and headers after creation/update/delete requests. Note the headers for resources created. Optionally you could configure to return the content ( RepositoryRestConfiguration.returnBodyOnCreate & RepositoryRestConfiguration.returnBodyOnUpdate) using if you prefer.

Related

How to make Spring Boot API accept a client's custom 'x-www-form-urlencoded' format for the nested objects parsing

The Spring boot API could not parse the x-www-form-urlencoded type request since the client posted the data differently than what Java supports.
The incoming request body looks like below:
DeliveryPostCode=TEST123000&EnableCOD=false&Basket%5B0%5D%5BId%5D=383293820&Basket%5B0%5D%5BCategory%5D=Smartphone
In which a Basket is an object which has Id, Category as fields.
Decoding the above URL gives -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0][Id]=383293820&Basket[0][Category]=Smartphone
which does not work for Java
If we can get a request which looks like below, It works for Java -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0].Id=383293820&Basket[0].Category=Smartphone
Since the client is something which we cannot afford to change. We have to do some manipulation in Java before Controller takes this
OR
a modify in middleware node js layer we have between client & server which acts as a proxy.
Please suggest.

REST API response handling when 1 of many endpoint fails

I have a Java Application(REST API, Jersey) that orchestrates a lot of backend calls for the front end.
It aggregates data from all the backend systems and then returns a JSON response.
Now I have to integrate one more backend system, so I added the response from the new backend API into the Aggregate response.
This new data is not super important so I don't want to fail the entire response if this new Call fails.
What is the best elegant way to do this and also inform the clients that one of the calls failed?

Best way to use Websocket with Spring Boot and Vuejs

I try to use Websocket with spring boot backend (as an API) and Vuejs frontend.
I take a simple use case to expose my question. Some users are logged on my website, and there is a messaging feature. User A send a message to User B. User B is actually logged, and I want to notify User B that a new message is arrived.
I see 3 ways to do it with websockets :
1 - When User A send message, an Axios post is call to the API for saving message, and, if the Axios response is success, I call something like
this.stompClient.send("/app/foo", JSON.stringify(bar), {})
2 - When User A send message, I only call something like
this.stompClient.send("/app/foo", JSON.stringify(bar), {})
and it's my controller's method (annotated with #MessageMapping("/xxxx") #SendTo("/topic/yyyy")) that call facade, service, dao to first, save message, then return message to subscribers
3 - I keep my actuals controllers, facade, services and DAO, and juste add when save is successfull something like :
#Autowired SimpMessagingTemplate webSocket;
...
#GetMapping("/send-message")
public ResponseEntity sendMessage(#AuthenticationPrincipal User user, ....) {
service.saveMessage(....);
webSocket.convertAndSend("/ws/message-from", message);
without a new controller contains #MessageMapping("/xxxx") #SendTo("/topic/yyyy"). User B is just subscibed to "/ws/message-from"
Could you help me.
In the 3 way there is a good method ?
Thanks you.
The one and two method has no much difference as you use axios from npm for sending request and the other one you can directly,while the third one you use controller,and facade dao at single place.it is about architecture and how you wanna send your requests for your framework,as a requirement.
They serve best at their level,till you come with specific requirement.
The suggestion would be to use axios.
It has advantages:
supports older browsers (Fetch needs a polyfill)
has a way to abort a request
has a way to set a response timeout
has built-in CSRF protection
supports upload progress
performs automatic JSON data transformation
works in Node.js

Spring boot - Threads / Feign-Client / Messaging / Streamlistener

We struggle to find a solution for the following scenario:
Situation
Receive a message via Spring Cloud Streamlistener
Invoke a REST-Service via Feign-Client
We have configured several Feign-RequestInterceptor to enrich
request header data.
We want to avoid passing every request header on the method call and like the central configuration approach of the request interceptors.
Problem:
How to access data from a specific message, which contains informations, that need to be added to every request call via the Feign-RequestInterceptor.
We don't have a Request-Context, as we come from a message.
Can we be sure , that the message consumption and the REST call is happening on the same thread? If yes, we could use the NamedThreadLocal to store the information.
Yes, unless you hand off to another thread in your StreamListener, the rest call will be made on the same thread (assuming you are using RestTemplate and not the reactive web client).

In Spring Controller before sending the response to UI, Can we send the response to a class?

I am tying to remove the httpsession from the cookies in the browser. In that scenario i am trying to point the response from the controller to a class before sending it to the UI. Can anyone please help?
The solution will be to implement a custom filter which will invoke request before Controller is invoking the request.
The following tutorial will show how to register a custom filter with spring .
http://www.mkyong.com/spring-mvc/how-to-register-a-servlet-filter-in-spring-mvc/

Categories

Resources