My Rest With Spring does not work

-2

The part of Rest I've circled almost all google and I still can not solve. the problem is exactly that when I make the query or I give a sava postman day or straight through the url to see if Json returns and nothing happens just the 404 error ..

follow my Code .. (My Class that I take care of my Rest)

package br.gov.sp.fatec.web.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
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 org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;

import br.gov.sp.fatec.model.Usuario;
import br.gov.sp.fatec.service.IServiceUsuario;
import br.gov.sp.fatec.service.UsuarioServiceImpl;
import br.gov.sp.fatec.view.View;



@RestController
@RequestMapping(value = "/usuario")
public class UsuarioWebController {




    @Autowired
    IServiceUsuario serviceusu;


    public void setUsuarioService(IServiceUsuario serviceusu) {
        this.serviceusu = serviceusu;
    }


    @RequestMapping(value="usuario/{nome}")
    public ResponseEntity<Collection<Usuario>>listarUsuarioNome(@PathVariable("nome") String nome){
        return new ResponseEntity<Collection<Usuario>>(serviceusu.buscar(nome),HttpStatus.OK);

    }


    @RequestMapping(value = "usuario/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
            @JsonView({View.All.class, View.Alternative.class})
        // E possivel indicar o status por anotacao, mas sera fixo, sem possibilidade de tratar erros
            @ResponseStatus(HttpStatus.CREATED)
            public Usuario save(@RequestBody Usuario usuario, HttpServletRequest request,HttpServletResponse response) {

            usuario = serviceusu.salvar(usuario);
            response.addHeader("Location", request.getServerName() + ":" + request.getServerPort() +
            request.getContextPath() + "/usuario/getById?id=" + usuario.getId());
            return usuario;

            }



}

(My User Class)

package br.gov.sp.fatec.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonView;

import br.gov.sp.fatec.view.View;



@Entity
@Table(name= "new")
public class Usuario  {



    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="idusuario")
    @JsonView({View.All.class, View.Alternative.class})
    private long id;


    @Column(name="usuario_nome")
    private String nome;


    @Column(name="usuario_senha")
    private String senha;

    public Usuario() {
        super();
        this.id = id;
        this.nome = nome;
        this.senha = senha;
    }


    public long getId() {
        return id;
    }


    public void setId(long id) {
        this.id = id;
    }


    public String getNome() {
        return nome;
    }


    public void setNome(String nome) {
        this.nome = nome;
    }


    public String getSenha() {
        return senha;
    }


    public void setSenha(String senha) {
        this.senha = senha;
    }






}

(My Class) Repository

package br.gov.sp.fatec.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

import br.gov.sp.fatec.model.Usuario;

@Service
public interface UsuarioRepository extends CrudRepository<Usuario, Long> {

    public Usuario findByNome(String nome);

    public Usuario findTop1ByNomeContains(String nome);

    public List<Usuario> findByIdGreaterThan(Long id);

    public List<Usuario> findByAutorizacoesNome(String nome);

    public List<Usuario> findByNomeContainsIgnoreCase(String nome);

    @Query("select u from Usuario u where u.nome like %?1%")
    public List<Usuario> buscaUsuario(String nome);
}

(My IService Interface)

package br.gov.sp.fatec.service;

import java.util.List;

import br.gov.sp.fatec.model.Usuario;
import br.gov.sp.fatec.repository.UsuarioRepository;

public interface IServiceUsuario extends UsuarioRepository {


    public List<Usuario> buscar(String nome);
    public Usuario buscar(Long id);
    public List<Usuario> todos();
    public Usuario salvar(Usuario usuario);

}

My Class

package br.gov.sp.fatec.service;


import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import br.gov.sp.fatec.model.Usuario;
import br.gov.sp.fatec.repository.UsuarioRepository;


@Service("IServiceUsuario")
public  class UsuarioServiceImpl implements IServiceUsuario {


    @Autowired
    private UsuarioRepository usuarioRepo;

    @Override
    public List<Usuario> buscar(String nome) {
        return usuarioRepo.findByNomeContainsIgnoreCase(nome);
    }

    @Override
    public Usuario buscar(Long id) {
        return usuarioRepo.findOne(id);
    }

    @Override
    public List<Usuario> todos() {
        List<Usuario> retorno = new ArrayList<Usuario>();
        for(Usuario usuario: usuarioRepo.findAll()) {
            retorno.add(usuario);
        }
        return retorno;
    }

    @Override
    public Usuario salvar(Usuario usuario) {
        return usuarioRepo.save(usuario);
    }

    /**
     * @return the usuarioRepo
     */
    public UsuarioRepository getUsuarioRepo() {
        return usuarioRepo;
    }

    /**
     * @param usuarioRepo the usuarioRepo to set
     */
    public void setUsuarioRepo(UsuarioRepository usuarioRepo) {
        this.usuarioRepo = usuarioRepo;
    }

    @Override
    public Usuario findByNome(String nome) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Usuario findTop1ByNomeContains(String nome) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Usuario> findByIdGreaterThan(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Usuario> findByAutorizacoesNome(String nome) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Usuario> findByNomeContainsIgnoreCase(String nome) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Usuario> buscaUsuario(String nome) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long count() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void delete(Long arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(Usuario arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(Iterable<? extends Usuario> arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void deleteAll() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean exists(Long arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Iterable<Usuario> findAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Iterable<Usuario> findAll(Iterable<Long> arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Usuario findOne(Long arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <S extends Usuario> S save(S arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <S extends Usuario> Iterable<S> save(Iterable<S> arg0) {
        // TODO Auto-generated method stub
        return null;
    }


}

(My Applications Context)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">       

    <context:component-scan base-package="br.gov.sp.fatec" /> 

    <jpa:repositories base-package="br.gov.sp.fatec.repository"/>


    <!-- Datasource -->
    <bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/sistec" />
        <property name="username" value="root" />
        <property name="password" value="fatec" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="br.gov.sp.fatec.model"
        p:dataSource-ref="dataSource"
        >
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false" />
                <property name="showSql" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            </bean>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven />


</beans>

(Meu Web.xml)

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Spring Rest</display-name>

  <!-- Add Support for Spring -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

.
    
asked by anonymous 29.11.2016 / 12:39

1 answer

0

Try the URL host:porta/contexto/usuario/usuario/save , because that's how you mapped the route according to your controller, if you want to remove this / repeated user just take "%" of the% desired method.

Example:

@RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    
29.11.2016 / 13:41