Response Em Restfull

Asked

Viewed 28 times

-2

Good evening, I’m new to Webservice and I have the following question, how do I make a response to the class created in java?

So far I’ve done it in my code:

package alarme;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BotaoController {

    public BotaoController(){
        Botao teste = new Botao(1,true);
    }

    @RequestMapping(value = "/botao/{id}", method = RequestMethod.GET)



}   

1 answer

0

Simply declare a Java method that takes the parameters of the request you will use and returns what you want:

@GetMapping("/botao/{id}")
public Botao getButtonById(@PathVariable Long id) {
    Botao teste = new Botao(1,true);
    return teste;
}

Spring allows you to declare these methods with your own return types (Botao in the above example), or encapsulated in a ResponseEntity for cases where you need to return other status:

@GetMapping("/botao/{id}")
public ResponseEntity<Botao> getButtonById(@PathVariable Long id) {
    Botao botao = userRepository.getOne(id);
    if(botao == null) {
        return ResponseEntity.notFound();
    }

    return ResponseEntity.ok(botao);
}

To official documentation has plenty of examples on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.