Escolar Documentos
Profissional Documentos
Cultura Documentos
NO
exemplo anterior, criamos um WS buscando todas as pessoas.
package com.aulawb.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aulawb.model.Pessoa;
import com.aulawb.repository.PessoaRepository;
@Controller
@RequestMapping(path="")
public class PessoaController {
@Autowired
private PessoaRepository pessoaRepository;
if(pes == null){ // se essa pessoa for nula, sigfica que não tem
ninguém com o id informado cadastrado no banco.
return new ResponseEntity(HttpStatus.NOT_FOUND); // se essa
pessoa for nula, retornamos o Status NOT FOUND do HTTP, ou seja, dizendo que essa
pessoa não foi encontrada.
}
return new ResponseEntity<Pessoa>(pes, HttpStatus.OK); // caso essa
pessoa buscada exista no banco, retornamos ela junto de uma mensagem HTTP de
sucesso.
}
package com.aulawb.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aulawb.model.Pessoa;
import com.aulawb.repository.PessoaRepository;
@Controller
@RequestMapping(path="")
public class PessoaController {
@Autowired
private PessoaRepository pessoaRepository;
package com.aulawb.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aulawb.model.Pessoa;
import com.aulawb.repository.PessoaRepository;
@Controller
@RequestMapping(path="")
public class PessoaController {
@Autowired
private PessoaRepository pessoaRepository;
-----------------------------------------------------------------------------------
-------------------------------------------------------------
** IMPORTANTE **
=> Agora vamos aprender a resolver um erro muito comum no web service. Se em algum
momento nós não pararmos um servidor antes de inicializar outro, o web service
acaba tentando abrir os dois paralelamente e aí dá um erro: porta já está em uso.
Quando isso acontece, ele não deixa a gente fechar o que está em uso. Quando isso
acontecer, temos que ir no terminal e fazer o seguinte:
=> Isso vai finalizar o processo, assim permitindo que possamos executar novamente
o web service.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
atualizando pessoa:
package com.aulawb.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aulawb.model.Pessoa;
import com.aulawb.repository.PessoaRepository;
@Controller
@RequestMapping(path="")
public class PessoaController {
@Autowired
private PessoaRepository pessoaRepository;