Spring Integration - http inbound - spring

im trying to make a http inbound but when i print my message the payload its empty, what am i missing here?
#Slf4j
#Component
public class HttpInboundGateway {
#Bean
public IntegrationFlow inGate1() {
log.info("Initializing inbound gateway...");
return IntegrationFlows.from(Http.inboundChannelAdapter("/")
.requestPayloadType(PayloadObj.class))
.channel("transfer_next_channel")
.get();
}
#Bean
#ServiceActivator(inputChannel = "transfer_next_channel")
public MessageHandler handler() {
return new MessageHandler() {
#Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println("myHandlerPayload: " + message.getPayload());
System.out.println("myHandlerHeader: " + message.getHeaders());
}
};
}
}
PayloadObj class expected:
#Data
#NoArgsConstructor
public class PayloadObj {
String name;
String job;
}
EDIT 1: As requested
Curl from Postman
curl --location --request GET 'http://localhost:9090/' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"Marcos",
"job":"Dev Jr"
}'
Print from handler:
myHandlerPayload: {} --- Payload
myHandlerHeader: {content-length=46, http_requestMethod=GET,
host=localhost:9090, http_requestUrl=http://localhost:9090/,
connection=keep-alive, id=e67aef3d-938a-7ac3-eb7d-ddaf9cd74f4e,
contentType=application/json;charset=UTF-8, accept-encoding=gzip,
deflate, br, user-agent=PostmanRuntime/7.28.4, accept=*/*,
timestamp=1642168826837}

curl --location --request GET 'http://localhost:9090/'
You do GET request. This way the body of the request is ignored.
And only request params are transferred as payload. Since you don't have those as well, that means the payload is empty Map.
Consider to use POST or PUT instead.
And learn more about an HTTP protocol and its method specifics.

Related

Java springboot POST request giving 404

I am trying a POST request in POSTMAN but even though its reaching the tomcat server node, I get following error in my localhost_access.log file
"POST /app/MyService/myControllerMethod HTTP/1.1" 404 1010
My Controller class is something like this :
#Controller("myServicecontroller")
#RequestMapping({"/MyService"})
public class MyServiceController {
#RequestMapping(value = {"myControllerMethod"}, method = {RequestMethod.POST})
public String myControllerMethodBackgroundCallBack(HttpServletRequest httpReq,
#RequestBody String request) {
// rest piece of code
}
}
Now my postman curl I am trying with empty data (tried with some value also) but get above 404 error response
curl --location --request POST 'http://my-ip-address:8080/app/MyService/myControllerMethod' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'My-Header-One: lskdnansdlknalkasndl' \
--header 'My-Header-Two: sadasdsa' \
--data-raw '{}'
What am I doing wrong? (app in above url is my service which works fine in other requests)
Same thing when I try with following code it is able to hit the api 200
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestBody(requestString);
httpClient.setConnectionTimeout(httpReadTimeOut);
httpClient.executeMethod(postMethod);
I have successfully replicated this issue and found the root cause.
Root Cause
#ResponseBody annotation is missing in myControllerMethodBackgroundCallBack method.
Fix
#RequestMapping(value = {"myControllerMethod"}, method = {RequestMethod.POST})
#ResponseBody
public String myControllerMethodBackgroundCallBack(HttpServletRequest httpReq,
#RequestBody String request) {
// rest piece of code
}
}
Why?
#ResponseBody annotation is required with #Controller annotation and if use #RestController annotation then #ResponseBody annotation is not required.
In Short-
#RestController = #Controller + #ResponseBody
You can read more about #Controller and #RestController here https://medium.com/#akshaypawar911/java-spring-framework-controller-vs-restcontroller-3ef2eb360917

Spring Webclient Post Method `BadRequest` error for a request that works in Postman (and curl)

I'm calling an external API via WebClient to create a resource. But getting WebClientResponseException$BadRequest while testing from Postman. Here are few things I've tried without success:
Building URI path from uribuilder
Coverting entity to MultiValueMap before inserting into the request
Controller:
#PostMapping("/createMessage")
public String createMessage(#RequestBody Message request) {
return service.createMessage(request);
}
Service:
public String createMessage(Message requestBody) {
ObjectMapper mapper = new ObjectMapper();
MultiValueMap requestBodyForm = new LinkedMultiValueMap<String, Object>();
Map<String, Object> requestBodyMap = mapper.convertValue(requestBody, new TypeReference<Map<String, Object>>() {});
requestBodyForm.setAll(requestBodyMap);
String id = webClient.post()
.uri(properties.getMessageUrl())
.contentType(MediaType.APPLICATION_JSON)
.headers( headers ->
headers.setBasicAuth(user,pass))
.body(BodyInserters.fromValue(requestBodyForm))
.retrieve()
.bodyToMono(String.class)
.block();
return id;
}
Any direction where might be the problem would help a lot.
The external service is an IoT device controller API, with predefined message format:
$ curl --location --request POST 'http://external-service/message' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic xxxxxxxxxxxx' \
--data-raw '[{
"message": "Testing service reachability",
"defaulttext": "This is Message Zone",
"startDate": "2021-05-25 15:20:00",
"duration":10,
...
}]'
198

Spring ControllerAdvice how to return Json or html page according to Accept request header?

Now, App can return a cutstom 404 page.
#ControllerAdvice
class GlobalControllerExceptionHandler {
#ResponseStatus(HttpStatus.NOT_FOUND)
#ExceptionHandler(NoHandlerFoundException.class)
public String handle404(WebRequest request) {
return "/exceptions/404";
}
}
And I wonder how should I do, if I want to get a JSON when Accept request header is Accept: application/json, like this:
$> curl -H "Accept: application/json" http://localhost:8080/no-such-page
{"timestamp":"2018-04-11T05:56:03.845+0000","status":404,"error":"Not Found","message":"No message available","path":"/no-such-page"}```

How to mimic curl REST call with Spring's RestTemplate?

Have an external Restful Web Service which takes in JSON payloads if its is more than one input but if its a single input it just needs the value.
For example, for multiple inputs, this works:
curl -H "Content-Type: application/json" -X POST -d '{ "amount": 10000, "interestRate": ".28", "term": "12", "state": "Georgia"}' http://localhost:8080/webservices/REST/sample/loan
Returns:
Approved
For single inputs:
curl -H "Content-Type: application/json" -X POST -d "18" http://localhost:8080/webservices/REST/sample/age
Returns:
Approved
Using Spring Boot, tried to create a JUnit test, in order, to see if I can post to this external service using Spring's RestTemplate API.
public void RestWebServiceTest {
private RestTemplate restTemplate;
private HttpHeaders headers;
#Before
public void setup() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
}
#Test
public void validLoan() {
final String uri = "http://localhost:8080/webservices/REST/sample/Loan";
Map<String, String> input = new HashMap<>();
input.put("amount", "10000");
input.put("interestRate", ".28");
input.put("term", "12");
input.put("state", "Georgia");
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}
#Test
public void validAge() {
final String uri = "http://localhost:8080/webservices/REST/sample/age";
Integer input = 18;
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}
#Test
public void validCountry() {
final String uri = "http://localhost:8080/webservices/REST/sample/country
String input = "US";
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}
}
All of these work except for the validCountry() test method:
org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
This is strange because this curl command works for the same call:
curl -H "Content-Type: application/json" -X POST -d 'US' http://localhost:8080/webservices/REST/sample/country
Returns:
Approved
Question(s):
How can one mimic the rest call for the country (see above curl command) inside the validCountry() test method?
Do I need to add or change a different value for HTTP Headers (inside setup() method)?
Don't understand that validAge works by using the Integer wrapper class but the String doesn't?
Is there a better way to do this using Spring's RestTemplate API?
Thank you for taking the time to read this...
You need to set the Content-Type to application/json. Content-Type has to be set in request. Below is the modified code to set the Content-Type
#Test
public void validCountry() {
final String uri = "http://localhost:8080/webservices/REST/sample/country";
String input = "US";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<String>(input, headers);
String result = restTemplate.postForObject(uri, request, String.class);
assertEquals("Approved", result);
}
Here, HttpEntity is constructed with your input i.e "US" and with headers.
I think this answers you questions 1,2 and 4. But not 3.

Unsupported Media Type in jersey RESTfull api

I am trying to build a RESTfull API using the Jersey library but it gives me an exception.
Here is my Docs class
#XmlRootElement
#XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Docs {
#XmlElement(name = "field")
public String field;
#XmlValue
public String content;
}
#Path("/update")
public class Update {
#POST
#Path("/xml")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.APPLICATION_XML)
public String createIndexXML(Docs docs)
throws Exception {
System.out.println(docs.content);
return "It works";
}
}
If I try to check it using CURL it throws Error 415 Unsupported Media Type
curl -XPOST "http://localhost:8089/update/xml" -d '<docs>
<field>title</field>
</docs>'
You need to add the content type to your request header. Add -H "Content-Type: application/xml" to yourcurl` call.
I think you're also going to find that there are problems with your annotations on your bean - but that's another issue...
This should work:
curl -H "Content-Type: application/xml"
-X POST -d "<docs><field>title</field></docs>" "http://localhost:8089/update/xml"
You should also try Accept: application/xml; watch out for defining both #Produces and #Consumes! See Using #Consumes and #Produces to Customize Requests and Responses.

Resources