How to properly connect to my VM to get json? - spring

I'm new in Spring and I have a problem when I run Spring WEB in VM.
Test on local computer works:
I run app mvn spring-boot:run -Dspring-boot.run.profiles=test and check request:
curl -X POST ``http://localhost:8080/api/v1/dictionary/yandex-alice-skill`` -H "Content-Type: application/json" -d "{}"
and get {"response":{"text":"Hi! I can help you to learn words.","end_session":false},"version":"1.0"}
When I run app in VM and try to get json in my VM everything works fine too:
BUT when I try to get json in my local computer I get 404
Page have 404, but why I can't get json? How to properly connect to my virtual machine?
Controller:
#RestController
#Slf4j
#RequestMapping("/api/v1/dictionary/yandex-alice-skill")
#RequiredArgsConstructor
public class DictionaryController {
#Autowired
private DictionaryService dictionaryService;
#PostMapping
public #ResponseBody talkYandexAlice(
#RequestBody YandexAliceRequest request) {
return dictionaryService.talkYandexAlice(request);
}
}
I want to get json, but I get HTML from server.

Related

Client authentication failure on Neo4J

I basically followed the steps described here: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#configure-spring-boot-project
My application.properties contains the following:
spring.neo4j.uri=neo4j://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=verySecret357
I have a Neo4jConfiguration Bean which only specifies the TransactionManager, rest is (supposedly) taken care of by spring-boot-starter-data-neo4j:
#Configuration
public class Neo4jConfiguration {
#Bean
public ReactiveNeo4jTransactionManager reactiveTransactionManager(Driver driver,
ReactiveDatabaseSelectionProvider databaseNameProvider) {
return new ReactiveNeo4jTransactionManager(driver, databaseNameProvider);
}
}
Neo4j (5.3.0) runs in a Docker container I started with
docker run -d --name neo4j -p 7474:7474 -p 7687:7687 -e 'NEO4J_AUTH=neo4j/verySecret357' neo4j:4.4.11-community
I can access it through HTTP on my localhost:7474 and can authenticate using the credentials above.
Now, when I run my springboot app and try to create Nodes in Neo4j, I keep getting the same exception:
org.neo4j.driver.exceptions.AuthenticationException: The client is unauthorized due to authentication failure.
Running in debug, it however seems the client authentication scheme is correctly set:
Any thoughts on what I might be doing wrong ?
Edit: one thing though, I would assume that the "authToken" would contain a base64-encoded String (username:password) as the scheme is basic auth. It looks like it's not the case (using neo4j-java-driver:5.2.0).
Edit: seems to be related to the Docker image. A standalone neo4j instance works fine.

How to debug quarkus lambda locally

I am beginner to Quarkus lambda and when I am looking for how to debug the Quarkus lambda then everyone is showing with REST API endpoints, is there any way to debug the Quarkus app using lambda handler ?
I know how to start the app in dev mode but I am struggling with invoking the handler method.
You can use SAM CLI for local debugging and testing. Here is the official documentation from quarkus.
It's really important that you follow the sequence.
Step-1:
sam local start-api --template target/sam.jvm.yaml -d 5005
Step-2:
Hit your API using your favourite rest client
Step-3
Add a Remote JVM debug configuration in your IDE, set your breakpoints and start debugging.
You can actually just add a Main class and set up a usual Run Configuration.
import io.quarkus.runtime.annotations.QuarkusMain;
import io.quarkus.runtime.Quarkus;
#QuarkusMain
public class Main {
public static void main(String ... args) {
System.out.println("Running main method");
Quarkus.run(args);
}
}
After that, just use curl or Postman to invoke the endpoint.
By default, the lambda handler starts on port 8080.
You can override it by passing
-Dquarkus.lambda.mock-event-server.dev-port=9999
So the curl will look like:
curl -XGET "localhost:9999/hello"
if the definition of the resource class looks like:
#Path("/hello")
public class GreetingResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello jaxrs";
}
}
Add a breakpoint in the Resource class and start the Main class in Debug mode. This will actually pause during a debug on a breakpoint.
You can just run mvn quarkus:dev and connect a remote debugger to it on port 5005 as shown in this image
Once quarkus is started in dev mode and you connect the remote debugger you can use Postman to send a request. Your breakpoints will be evaluated.

Controller returns 404 when Spring Boot is started from WSL

I'm using WSL2 with Ubuntu 18.04 and can't figure out why I can't call my endpoints when I start the Spring Boot Application within WSL2.
#RestController
#RequestMapping("/task")
#AllArgsConstructor
public class TaskController {
#GetMapping
public String add() {
return "Hello";
}
}
I send the following Request with Postman from my Windows machine:
http://localhost:8080/task
Response:
{
"timestamp": "2021-09-14T19:52:27.350+00:00",
"status": 404,
"error": "Not Found",
"path": "/task"
}
Response when I start the Spring Boot Application on Windows:
Hello
I think is because the wsl have another IP. So in OS Windows you have to put
http://<ip_wsl>:8080/task
.
In wsl2 you can normally only run the springboot application through a file.jar or docker image that is inside wsl2, so if you are using docker follow the steps below.
Try starting the image by creating another container with the following command:
docker run -p 8080:8080 id/project:latest
Where:
id = your ID of docker
project = name of repository/project
latest = version
Now try to post again by ip http://localhost:8080/task

Spring boot: forward-headers-strategy has no effect

I have simple rest controller
public void getMyIp(HttpServletRequest request)
{
final var ip = request.getRemoteAddr();
....
}
And I emulate request through proxy server
curl --location --request GET 'localhost:8080/api/myIp' \
--header 'X-Forwarded-For: 10.10.10.10' \
--header 'X-Real-Ip: 10.10.10.10'
I changed strategy in application.yml
server:
forward-headers-strategy: FRAMEWORK
The application runs from IDE with build-in tomcat server.
Why I gets my real ip address?
UPD: I changed strategy to native, and it works now!
It need to use strategy NATIVE
server:
forward-headers-strategy: NATIVE

Spring Cloud Config refresh configurations

Is it possible to refresh the configurations calling a java method instead to use the REST api:
curl localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
You can use the ResartEndpoint class from spring-cloud-context:
#Autowired
private RestartEndpoint restartEndpoint;
...
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
This is how #alexbt suggests to do it. But note that the spring cloud documentation also says you can refresh individual beans provided they are RefreshScope.

Resources