2

My question is very basic.

I have the following html:

<form action="/login" method="GET">
  <div>
    Id: <input type="text" name="id" value="id"/>
    Name:  <input type="text" name="name" value="name"/>
  </div>
    <button>Login</button>
 </form>

And the following @GetMapping method to which I want to connect:

 @GetMapping("/login")
  public String someLogin(LoginObject obj, HttpServletRequest request) {
    //do something
  }

LoginObject is composed from two fields: id and name, which I am taking from the UI. How do I convert two strings to my LoginObject and then pass it to the @RestController

1
  • @Taplar I edited my question as per your comment Commented Nov 1, 2018 at 16:49

1 Answer 1

2

First of all you need to decide form method (POST/GET). Than annotate your rest controller. Here is the small example for you;

<form action="/demo/add" method="get">
    <label>Username</label><input type="text" id="username" name="username"> 
    <label>Email</label><input type="email" id="email" name="email">
    <button>Send</button>
</form>

And Rest Controller;

@RestController
@RequestMapping(path="/demo")
public class WebController {

@Autowired
private UserRepository userRepository;

@GetMapping(path="/add")
public @ResponseBody String addUser(@RequestParam String username, @RequestParam String email) {
    User u = new User();
    u.setUsername(username);
    u.setEmail(email);
    userRepository.save(u);
    return "SUCCESS!";
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.