• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Spring Call a method from main

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

please, how could I call a method in Spring Boot? Having this main:



And been the method in another Class



From the browser I call localhost:8080/send

It reaches the sendL method but  the  lObject is empty. It looks to me like I am no passing any values thougth the arguments.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the moment I am passing the parameter like this


From the browser I call

But as the lObject isnt a String I get the error;



Then how should I send an object to a method with Spring?

Also in the



I have instantiated a mock object like this



How could I get an object with a value in the called method?





 
Bartender
Posts: 1309
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're sending a complex object, you should use POST, not GET.



Try Postman or  Advanced REST client Chrome's extension to work with REST services requireng to POST entities (for example; I used to use those tools, I presume many others are available)
 
Sheriff
Posts: 22209
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need @RequestBody if you do a form POST and these form fields map to properties of the method argument. It possibly also works with GET request and query parameters, never tried it. @RequestBody is mostly used with JSON or XML request bodies.
 
Claude Moore
Bartender
Posts: 1309
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You don't need @RequestBody if you do a form POST and these form fields map to properties of the method argument. It possibly also works with GET request and query parameters, never tried it. @RequestBody is mostly used with JSON or XML request bodies.


Yes Rob, you're right. I presumed that OP was talking about a REST API, that's why I used @RequestBody. About using GET and query parameters, it should work,  but somehow I presumed that send() method was intended to create an entity, so that I assumed that a POST would have been a better choice.
 
Rob Spoor
Sheriff
Posts: 22209
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, POST would be a better option.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic