#How to return a JSON response

1 messages · Page 1 of 1 (latest)

gritty wind
#

I am using react and spring boot for a full stack application
my browser console says the following.
I want a JSON response to displayed

amber summitBOT
#

<@&987246883653156906> please have a look, thanks.

violet furnace
#

I would have helped you if I was a professional in java

#

Can someone suggest me how can I master java

gritty wind
strange raptor
#

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);
}
amber summitBOT
strange raptor
#

sorry i didnt realize few things in the code, just fixed it

gritty wind
#

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?

amber summitBOT
strange raptor
#

btw you could just use Map.of("message", "...")

#

wait we arent in kotlin

#

frick

#

lol i keep forgetting

gritty wind
#

yep

#

like this?

strange raptor
#

anyways

#

exists Collections.singletonMap()

gritty wind
#

Map <String, String> = Map.of("message, "mg")

strange raptor
#
  1. I forgot no kotlin so you would use Collections.singletonMap()
#

not Map.of

#

and

gritty wind
#

Map.of is java

#

spring

strange raptor
#

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

gritty wind
#

ooh i see thank you for helping

violet furnace
#

Can u provide link or something similar please

#

And ping me when u do

low drum
#

we typically recommend mooc for learning java on this server 🙂

amber summitBOT
#

@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 than 2023.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

strange raptor