Why Rest End point is not showing any json data while using POST method in POSTMAN application? - spring-boot

I am trying to implement sample spring boot project and to ensure my endpoints are working properly, i'm using POSTMAN. When using POSTMAN , I am not able to see the response(i.e in Pretty) for a POST request. But the Status is 200 OK and I am able to see the result using GET request.
No Pretty response for POST request
GET Response ensuring that the previous POST request works fine
And my controller code is the following
#PostMapping("/message")
public Message createMessage(#RequestBody Message message)
{
return service.createMessage(message);
}
Can anyone help me to find out why I am not able to see the result while using POST method please?

Like Rafael says it is good to return a Response with the object entity. I haven't been working with Spring myself but with JavaEE and in JavaEE it is perfectly possible to return the object directly without using a Response. I use Responses anyways though, because it is much nicer to work with, and you can create your own custom responses and status codes.
Maybe check if your createUser service actually returns a message.

I don't know much about Spring, but usually what works for me is using a ResponseEntity as the object returned by the function. Also, maybe you should use #RestController as the annotation to your class controller
#PostMapping("/message")
public ResponseEntity<Message> createMessage(#RequestBody Message message)
{
Message msg = service.createMessage(message);
return ResponseEntity.ok(msg);
}

Related

Spring boot Webclient's retrieve vs exchange

I have started using WebClient in my Spring boot project recently.
Can somebody throw some light on the differences/usages between exchange and retrieve method in WebClient.
I undertand that exchange returns Mono<ClientResponse> and retrieve returns ResponseSpec, I just want to know when/why I should use each one of them.
Much Thanks.
Adding to #JArgente's answer.
According to the official documentation of the retrieve() method:
Perform the HTTP request and retrieve the response body.
...
This method is a shortcut to using exchange() and decoding the response body through
ClientResponse.
and the exchange() method
Perform the HTTP request and return a ClientResponse with the response status and headers. You can then use methods of the response to consume the body:
The retrieve() method decodes the ClientResponse object and hands you the ready-made object for your use. It doesn't have a very nice api for handling exceptions.
However on the other hand the exchange() method hands you the ClientResponse object itself along with the response status and headers. With exchange method you get fine grained control over your response objects and a better way to handle the response object and the exceptions.
If you just want to consume some api go with retrieve().
If you want a better control over your response objects, headers and exceptions, go with exchange().
Update 1
Starting from Spring 5.3, the exchange() method is deprecated due to possible memory/connection leaks. exchangeToMono() or exchangeToFlux() can be used instead.
Thanks #rhubarb for the update.
According to spring Webclient api documentation the difference between the two is that exchange retrieve in addition to the body other http response information like headers and status, while retrieve only returns body information.
So If you only need the body information you should use retrieve, because it is a shortcut for exchange and then get the body, but if you need other information like http status you must use exchange.

Spring PostMapping return 401 without body

I want to make a Post to write some data into the database, but all needed information is stored on the server, so my Post service requires no body:
#PostMapping("foo")
public #ResponseBody
RestResponse writeFoo() {
// WRITE AND RETURN
}
If I try to make a post request to this service I receive 401 even if I pass a valid token. If I change my exposed service to a GetMapping all works as expected. It seems that I can't manage a Post request with an empty body.
I've tried adding some fake parameters as
RestResponse writeFoo(#RequestBody(required = false) String fake)
but without success.
Any idea?
The issue you explain is most commonly the cause of bad (or missing?) configuration.
Pay attention that i.e. GET method is allowed by default by your REST API, while you need to specify other method types (i.e. PUT and POST), otherwise it won't work out of the box due to CORS.
The part where GET method works while POST method doesn't is a strong hint towards missing/incorrect CORS configuration. You can fix it quickly by adding some CORS filter and setup your response headers.
The official documentation should give you a good start, if you don't know where to look for: Spring docs - enabling CORS
UPDATE:
The issue is successfully resolved, check comments section for more info.
Short story - back-end configuration for CORS/CSRF token was set up correctly in this particular case, the issue occurred due to missing header (CSRF token) on the angular/front-end part of the webapp.

Rest End point not showing any json data when using POSTMAN application

I am trying to implement sample spring boot project and trying to retrieve result using POSTMAN application. But when using POSTMAN , I am not able to see that response for a GET request. But I can see by using browser properly. POSTMAN returning "Expected ':' instead of 't'" as result. But I can see result properly by using browser.
And my controller code is like the following,
#GetMapping("/check")
public List<Users> check() {
return (List<Users>) userObj.findAll();
}
Can anyone help me to find out why I am not able to see result using POSTMAN application please?
You are saying to postman (for that matter any client) that your api is producing json and instead you are returning just a string.
Check your header you will have a field called
Content-Type → application/json;charset=UTF-8
All you need to do is ensure you are sending valid json or remove that header entry so clients do not try to read it as json.
Or just to check you are getting right data by changing format from json to text in postman
You server application needs to understand the incoming request type. can you define the Content-Type=application/json and give a try.

How to log Spring WebClient response

I'm new to Spring WebClient. Can someone advise the best way to log REST request and response from another webservice?
I've already seen an example of logging request within the question but also have to log a response and a request for a POST call.
how to log Spring 5 WebClient call
Thank you.
One option is to use the onStatus function. The advantage is that you can react differently on different status codes:
.onStatus(HttpStatus::is4xxClientError, res -> {
res.toEntity(String.class).subscribe(
entity -> log.warn("Client error {}", entity)
);
return Mono.error(new HttpClientErrorException(res.statusCode()));}
)
But be aware that this will log asynchronously, that means it might log after you already logged something different. I'm using this way right now but I know it is not perfect, so I will be happy to see better suggestions.

Override response of POST in Django Rest Framework

I'm using Django Rest Framework's Generics (generics.ListCreateAPIView), when I make a POST request I get a response of Http code (200/400/..etc.) and a JSON showing the posted data, I need to know how can I override the response to get a custom response.
Note that I use
def perform_create(self,serializer):
return Response(<my response>)
to override the POST request handling but I still get the same response
The response from perform_create is ignored.
You'll likely want to override the create method using the mixins as example

Resources