Problemas com JSON

2 respostas
jsonajaxjava
W

Tenho uma aplicação e está dando alguns problemas com JSON na hora de autenticação.

415: HTTP Status [405] – [Method Not Allowed]

Type Status Report

Message Method Not Allowed

Description The method received in the request-line is known by the origin server but not supported by the target resource.

Apache Tomcat/9.0.0.M20

Os códigos que fazem a autenticação estão da seguinte forma:

function foundUser() {
	$.ajax({
		type: "POST",
		url: "rest/authenticationRest/searchUser",
		data: $("#authentication").serialize(),
		success:function(date) {
			alert("Teste");
		},error(err) {
			console.log(err);
			alert("Erro ao processar a requisição " + err.responseText);
		}
	});
}
package br.com.festivalRest.rest.jdbcinterface;

import java.util.List;
import br.com.festivalRest.objetos.User;

public interface FestivalDAO {
	public boolean searchUser(User user);
}
package br.com.festivalRest.rest.authentication;

import java.io.StringWriter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import com.google.gson.Gson;

public class UtilRest {

	public Response buildResponse(Object result) {
		Gson gson = new Gson();
		String json = gson.toJson(result);
		return Response.ok(json).build();
	}
	
	public Response buildErrorResponse(String str) {
		ResponseBuilder rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
		
		// Define o objeto que será uma mensagem retornada para o cliente
		rb = rb.entity(str);
		
		// Define o tipo de retorno deste objeto
		rb = rb.type("text/plain");
		
		return rb.build();
	}
}
package br.com.festivalRest.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import br.com.festivalRest.rest.jdbcinterface.FestivalDAO;
import br.com.festivalRest.objetos.User;

public class JDBCAuthenticationDAO implements FestivalDAO {
	private Connection conexao;
	
	public JDBCAuthenticationDAO(Connection conexao) {
		this.conexao = conexao;
	}
	
	public boolean searchUser(User user) {
		boolean checkUser = false;
		
		String comando = "select * from usuarios where login = ? and senha = ?";
		PreparedStatement p;
		ResultSet rs = null;
		
		try {
			p = this.conexao.prepareStatement(comando);
			p.setString(1, user.getLogin());
			p.setString(2, user.getSenha());
			rs = p.executeQuery();
			
			if(rs.next()) {
				checkUser = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return checkUser;
	}
}
package br.com.festivalRest.rest.authentication;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import br.com.festivalRest.bd.conexao.Conexao;
import br.com.festivalRest.jdbc.JDBCAuthenticationDAO;
import br.com.festivalRest.objetos.User;

@Path("authenticationRest")
public class AuthenticationRest extends UtilRest {
	
	public AuthenticationRest() {
	}
	
	@POST
	@Path("/searchUser")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes("application/json")
	public Response foundUser(String usuarioParam) {
		try {
			Conexao conec = new Conexao();
			Connection conexao = conec.abrirConexao();
			JDBCAuthenticationDAO jdbcAuthentication = new JDBCAuthenticationDAO(conexao);
			
			Gson gson = new Gson();
			User user = gson.fromJson(usuarioParam, User.class);
			
			jdbcAuthentication.searchUser(user);
			conec.fecharConexao();
			
			return this.buildResponse("OK");
		} catch(Exception e) {
			e.printStackTrace();
			return this.buildErrorResponse("Falha");
		}
	}
}

2 Respostas

L

Você criou alguma classe extendendo Application?

W

Sim, eu fiz uma verificação, e ele pelo JS dei um console.log no objeto, e está me retornando uma string ao inves de um JSON, está afetado está linha:

Gson gson = new Gson();
		User user = gson.fromJson(usuarioParam, User.class);
Criado 9 de junho de 2017
Ultima resposta 12 de jun. de 2017
Respostas 2
Participantes 2