Passing a header to custom actuator health endpoint - java

I have created a custom spring-boot actuator health endpoint for my application. Spring boot aggregates all these custom health endpoints into a one big json and returns it when I hit application/health url. Now I want to pass some information to the custom health end point that I have implemented in form of a header when I hit the application/health url. How can I achieve this?

If you at all want to pass a header , you might consider hitting the API (a GET API in this case) from any RestClient like PostMan or Insomnia. Simply hitting the url from the browser is actually making a GET request but you can only add path or request parameters in the url, for adding a request body or headers,you will need to use a REST client, or alternatively do it from the command line using curl.
Also, after reading the comments,i think what you need is,is to set spring.profile property and determine your code flow based on that- here's how you can set the profile-
1) set Java System Properties (VM Arguments)
java -jar -Dspring.profiles.active=test myapp.jar
2) set Program arguments
java -jar application.jar --spring.profiles.active=test

Related

Cannot work api request in Spring Cloud API Gateway

I have a problem about running the URLs shown below.
http://localhost:8041/users-ws/users/status/check (run)
http://localhost:8041/users/status/check (not run)
What I want to do is to run all these URL but only the first one runs.
How can I run both URLs?
How can I revise application properties file?
Here is my code snippet shown below in application.properties file in Spring Cloud API Gateway
server.port=8041
spring.application.name=api-gateway
eureka.client.service-url.defaultZone=http://localhost:8040/eureka
spring.cloud.gateway.routes[0].id=users-status-check
spring.cloud.gateway.routes[0].uri = lb://users-ws
spring.cloud.gateway.routes[0].predicates[0]=Path=/users-ws/users/status/check
spring.cloud.gateway.routes[0].predicates[1]=Method=GET
spring.cloud.gateway.routes[0].filters[0]=RemoveRequestHeader=Cookie
spring.cloud.gateway.routes[0].filters[1]=RewritePath=/users-ws/(?<segment>.*), /$\{segment}
Could you post your user-ws controller?
Anyway, try that:
spring.cloud.gateway.routes[0].predicates[0]=Path=/users/**
looks like your application name is "users-ws", and your request mapping is "/users", in this case, is not necessary put your application name in the path but we need to see more code, to be honest.

Is it possible to change the /health endpoint in spring-boot?

I have a spring-boot-web application and I am using actuator to check status of my application. Whenever I do /health I am able to check the status of my application. My doubt is, is it possible to change the /health endpoint to something like /ABC/health(I need to add context to /health). Is it possible or do I need to have a controller for this and need to handle it.
You can use a built-in property to expose all Actuator endpoints under different path. Just set it in your application.properties file:
management.context-path=/ABC
The health will be than available under /ABC/health.

HTML Encoding of request parameters

My application is a Spring MVC application, does not use annotations (its quite an old application though it uses spring 3.1.1).
I have a JSP page that can be invoked via a tool like curl or directly from the web browser URL. The URL accepts a request parameter called location
http://localhost:8080/testdomain/registeruser.do?mylocation=fr
On invoking the above url, I call an implementation of SimpleFormController that reads the request parameter "mylocation"
How do I html encode the request parameter "mylocation". Before reading the parameter from the request [request.getParameter("mylocation")] in my controller, would is suffice if I call HtmlUtils.htmlEscape(request.getParameter("mylocation"))
Please note there is no form backing the input parameter for mylocation, as outlined earlier I could call this from a command line tool or browser URL.
There are two different things:
1) URL encoding/decoding - it is only to allow passing GET parameters to the server. Encoding is needed because server need to escape some special characters "?" , "&" in query string.
It is usually handled by web server like Tomcat.
2) And to prevent XSS you can use Jsoup. It is only needed if you will present value from parameter on the web page.
Some frameworks, like JSF can prevent XSS automatically. They can escape HTML when it is displayed on the web page.

Using Spring Boot, how to separately handle exception between normal request and ajax request

I am implementing Java web application using Spring MVC and Spring Boot.
I would like to create a custom error page (For 404, 500 errors), I try to use the solution from here:
https://stackoverflow.com/a/27393578/2148445
and it works fine. However, it intercepts all the 404/500 errors even my ajax request. My web application is based on ajax request, so when I call API, it returns custom error page instead of json response.
I would like to:
when normal request >> go to custom error page.
when ajax request >> return error response as json object.
I try to find the other ways but I cannot get the good solution so far.
I tried to use #ExceptionHandler, but my colleague said that it will override Spring boot's.
Is there a way to solve this issue?
Thank you for your help.
A solution would be to send an "X-REQUESTED-WITH" header with value "XMLHttpRequest" when doing ajax calls from the browser.
On the backend side, you can check if this header with the given value is available to decide whether it was an ajax call or not.

How to get XML file from Java Request object before sending. Web services SOAP

I am building Java application for Online Web Services and let's call it application A . I got the WSDL file form the second party so I can communicate with their application and let's call it application B.
From the WSDL file I generate the Java classes needed which are Requests and Responses classes. Application A will send some request object after setting the needed parameters and excepting response object from application B.
The connection is established and both applications A and B are communicating with each other.
Question:
From application A how can I get the xml data(file or text) for the request object before sending it to application B?
As described the connection is done by passing Java object as request and I know that in some point this request will be converted to xml file. How to get it?
--- EDIT ----
Important Information is missing that may cause confusion.
I am generated the Java Classed have been generated using Axis framework
I don't have much reputation to post a comment, so here is my answer: If you aren't yet using some framework use Apache CXF, If you want to capture the request before sending it application , you can either use cxf interceptors there are some inbuilt interceptors which can do this or you can create a custom interceptor with correct phase ( e.g. post marshal)
The problem is solved by adding the following statements in the bindingStub class that has been auto generated from the WSDL file for the web-services you are trying to access.
String request = _call.getMessageContext().getRequestMessage().getSOAPPartAsString();
String response = _call.getMessageContext().getResponseMessage().getSOAPPartAsString();
These statements should be placed after the following method call _call.invoke otherwise you will get NullPointerException .
_call is a variable of type org.apache.axis.client.Cal and it is auto generated by Axis

Categories