#How to return a JSON response
1 messages · Page 1 of 1 (latest)
<@&987246883653156906> please have a look, thanks.
I would have helped you if I was a professional in java
Can someone suggest me how can I master java
I learnt from Telusko
you could just simply return "{\"msg\": \"Something\"}"
OR
The better solution
Create a record/pojo which will get automatically serialized into a json when returning
Like this:
public record TestApiResponse(String message) {
}
@PostMapping("/api/v1")
public ResponseEntity<TestApiResponse> handle() {
return new ResponseEntity<>(new TestApiResponse("Something"), HttpStatus.CREATED);
}
Detected code, here are some useful tools:
sorry i didnt realize few things in the code, just fixed it
Just asked GPT
response.put("message", "user already exists");
return new ResponseEntity<>(response, HttpStatus.CONFLICT);
Is this a better way or the one u gave?
Detected code, here are some useful tools:
Map<String, String> response = new HashMap<>();
response.put("message", "user already exists");
return new ResponseEntity<>(response, HttpStatus.CONFLICT);
btw you could just use Map.of("message", "...")
wait we arent in kotlin
frick
lol i keep forgetting
Map <String, String> = Map.of("message, "mg")
i messed it with mapOf in kotlin xd
so like, if you would ever want to reuse this approach, you should create a record instead
for example
public record GenericResponse(String message) {}
in the end, you can use both
ooh i see thank you for helping
What is telusko
Can u provide link or something similar please
And ping me when u do
we typically recommend mooc for learning java on this server 🙂
@violet furnace
For learning Java, we recommend MOOC.
It is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.
Visit MOOC here:
https://java-programming.mooc.fi
(the course is available in both English and Finnish)
- The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
The end of the course is marked by creating your own Asteroids game clone! - The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
- To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.
The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ, which we generally recommend.
- TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
- VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
- IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
IntelliJ requires installing a version no newer than2023.1- because the IntelliJ TMC Plugin doesn't work with newer installs.
The IntelliJ Community version is completely free and all you need to install the TMC plugin.
To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.
About the course - Java Programming
This is incorrect channel btw