Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How does @PostMapping work?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I know it's a very basic and broad question but how does postmapping work?

For a school project, I have to build a spring application, that has both a @getmapping and a @postmapping method (?), and I think I nailed the @getmapping part, but I have no idea how to do the other one.


For the getmapping part, my task was to write "Hello user" to localhost if localhost has no parameters, and write "Hello *name*" if localhost does have a name parameter.

For the postmapping, I should write a code that posts the following text backwards to localhost if it has no text parameter: " Have a nice day" and if it does have a text parameter, then the text written in the text parameter should be posted backwards.

I hope it's not too confusing and sorry for the cr@ppy english but I barely understand the task in my native language.

I'll post what I have so far:


I have a "Greeting.java" class:




And I have a "GreetingController.java" class:



Thank you for your help in advance and sorry for the long question.
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

greg mic wrote:. . . it's a very basic . . . question  . .

No, it isn't. Let's move you to the Spring forum, where we usually discuss that sort of question.
 
greg mic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

greg mic wrote:. . . it's a very basic . . . question  . .

No, it isn't. Let's move you to the Spring forum, where we usually discuss that sort of question.



Sorry for posting in the wrong forum
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think nothing of it.
 
Bartender
Posts: 2072
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Greg,
Back to your question , how does @PostMapping work?
You may want to search for tutorials about Restful web services. There are four operations, CRUD. C stands for "create", which is what post mapping do.
If you have a database, post is to create new data.
One of the references:
https://stackabuse.com/get-http-post-body-in-spring/
 
Saloon Keeper
Posts: 12893
280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

greg mic wrote:write "Hello user" to localhost


First off, it's important to get the vocabulary right. You don't "write to localhost". You "write a response" or you "write to a page hosted at localhost". When we talk about localhost, we mean the machine the software is currently running on.

if localhost has no parameters, and write "Hello *name*" if localhost does have a name parameter.


Again, localhost is a machine. It doesn't have parameters. You mean "if the URL contains a name argument".

Sorry for being nitpicky, but if you don't use the correct terminology it's going to be hard for people to help you.

Anyway, yes you nailed the GET mapping. Now let's take a look at the POST mapping.

For the postmapping, I should write a code that posts the following text backwards to localhost if it has no text parameter: " Have a nice day" and if it does have a text parameter, then the text written in the text parameter should be posted backwards.


It's extremely unfortunate that the assignment immediately teaches you wrong things.

POST messages are used when you want to make a change to an application, such as saving data to a database. When the action doesn't make any changes to the application, but only transforms your input and then spits it out again, you would use a GET message.

This is not your fault. Just keep in mind that POST is not normally used for the kinds of tasks like the one in your assignment.

What have you tried? What are you stuck on? Do you know how to trigger a POST request?
 
reply
    Bookmark Topic Watch Topic
  • New Topic