400 Bad request java server post @Consumes(Application / java)

时间:2015-03-22 20:34:29

标签: java json post

我在Android上有登录移动应用程序,人们可以通过添加用户名和密码登录。 单击“登录”按钮后,我将用户对象发送到我的服务器,只填写电子邮件(用户名)和密码。 这是我的目标:

@Entity
@Table(name="utente")
public class Utente implements Serializable {

@Id
@Column(name = "id")
private Integer id;

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

@Column(name = "cognome")
private String cognome;

@Column(name = "email")
private String eMail;

@Column(name = "password")
private String password;

@Column(name = "ultimo_accesso")
private Date ultimoAccesso;

public Utente(){

}

(这是我对象的java方面)下面有getter和setter。

我的代码如下:

@POST
@Path("login/docente")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response loginDocente(Utente utente){

   // Utente utente = utenteJson.getValue();JAXBElement<Utente> utenteJson

    try{
        UtenteController utenteController = new UtenteController(entityManager);
        Docente docente = utenteController.loginDocente(utente.geteMail(), utente.getPassword());
        if (docente == null) {
            return Response.noContent().build();} 

        else{

            docente.getUtente().setUltimoAccesso(new Date());
            return Response.ok().entity(docente).build();
        }
    }catch(Exception e){
        return Response.status(Status.FORBIDDEN).entity(e).build();

    }
}

但每次我写这段代码:

{
"eMail":"MyEmail",
"password":"MyPsw"
}

使用Advanced Rest Client Application,我收到此错误:

Status
500 Internal Server Error Show explanation Loading time: 1482
Request headers 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json 
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Response headers 
Server: GlassFish Server Open Source Edition 4.1 
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.7)
Content-Language: 
Content-Type: text/html 
Date: Sun, 22 Mar 2015 20:33:00 GMT 
Connection: close
Content-Length: 1154 

我真的不知道如何解决这个问题,任何想法?

@Edit: 我试图添加一个异常捕手,但我的问题是请求没有到达我的服务器,它被休息客户端停止,所以我的服务器既没有得到输入

1 个答案:

答案 0 :(得分:1)

您可以创建一个异常映射器,它可以告诉您确切的问题。我有同样的问题,因为默认情况下你没有看到有关解析和servlet异常的异常,它可能非常烦人。

只需在包中添加一个类,如:

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

    @Override 
    public Response toResponse(Throwable ex) {
        ex.printStackTrace();
    }
}

这会捕获所有异常,因此它可能对您有所帮助

相关问题