2014-07-20 48 views
1

主要问题是我无法使用Ajax或curl将新数据发布到数据库中。春季JPA无法理解此JSON。可能是什么原因?

错误我面临:

org.springframework.http.converter.HttpMessageNotReadableException: 无法读取JSON:意外字符( 'F'(代码102)):

内容Users.java的:UsersRepository.java的

package com.harmathuwebLogin; 


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; 

@Entity 
@Table (name="Users") 
public class Users { 

@Id 
@GeneratedValue (strategy = GenerationType.AUTO) 
private long id; 

@Column (name="firstname") 
private String firstName; 

@Column (name="lastname") 
private String lastName; 

@Column (name="username") 
private String userName; 

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

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public String getPassWord() { 
    return passWord; 
} 

public void setPassWord(String passWord) { 
    this.passWord = passWord; 
} 

@Override 
public String toString() { 
    String res = "{ \"firstName\" : " + this.firstName + " }"; 
    return res; 
} 

} 

内容:

package com.harmathuwebLogin; 

import java.util.List; 

import org.springframework.data.repository.PagingAndSortingRepository; 
import org.springframework.data.repository.query.Param; 
import org.springframework.data.rest.core.annotation.RepositoryRestResource; 

@RepositoryRestResource(collectionResourceRel = "users", path = "users") 
public interface UsersRepository extends PagingAndSortingRepository<Users, Long>{ 
List<Users> findByLastName (@Param("lastName") String lastName); 

boolean findByUserNameAndPassWord(@Param("userName") String userName, @Param("passWord") String passWord); 
} 

Application.java的内容:

package com.harmathuwebLogin; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 

@Configuration 
@ComponentScan 
@EnableJpaRepositories 
@PropertySource(value = { "classpath:application.properties" }) 
@Import(RepositoryRestMvcConfiguration.class) 
@EnableAutoConfiguration 
public class Application { 

public static void main(String[] args) { 
    SpringApplication.run(Application.class, args); 
} 
} 

此外,

If I try to go to "http://localhost:8080/users", it returns a valid JSON, also it returns correct json with "http://localhost:8080/users/1" 

问题是,当我尝试做

curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins", "userName" : "frodobaggins", "passWord" : "shireisheaven" }' http://localhost:8080/users 

它提供:

HTTP/1.1 400 Bad Request 
Server: Apache-Coyote/1.1 
Content-Type: application/schema+json 
Transfer-Encoding: chunked 
Date: Sun, 20 Jul 2014 08:05:53 GMT 
Connection: close 

{ 
    "cause" : { 
    "cause" : null, 
    "message" : "Unexpected character ('f' (code 102)): was expecting double-quo 
te to start field name\n at [Source: org.apache.catalina.connector.CoyoteInputSt 
[email protected]; line: 1, column: 5]" 
    }, 
    "message" : "Could not read JSON: Unexpected character ('f' (code 102)): was e 
xpecting double-quote to start field name\n at [Source: org.apache.catalina.conn 
[email protected]; line: 1, column: 5]; nested exception is com.f 
asterxml.jackson.core.JsonParseException: Unexpected character ('f' (code 102)): 
was expecting double-quote to start field name\n at [Source: org.apache.catalin 
[email protected]; line: 1, column: 5]" 

它表明这个错误在Eclipse控制台:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('f' (code 102)): was expecting double-quote to start field name 
+1

在这里看起来很可疑的是,你据称获得'appl对于所述请求而言,我不能相信,这是我无法相信的。不要认为这与JPA设置有关,而与杰克逊整合有关。 –

+0

嗨, 感谢您关注此事。 –

+0

嗨, 感谢您关注此事。问题可能在于schema/json。我正在使用Windows Power Shell,我知道这是一个耻辱。如果您注意到我的curl命令使用POST,它仍然将内容类型设置为schema + json,而不仅仅是application/json。另外,如果我从卷曲中删除-H头文件,那么我面临着一个不同的问题: org.springframework.http.converter。HttpMessageNotReadableException:没有合适的HttpMessageConverter发现通过应用程序的内容类型/ x-www-form-urlencoded将请求主体读入类com.harmathuwebLogin.Users类的对象中。 在此先感谢。 –

回答

2

我刚才运行你的代码,并与谷歌测试REST客户端张贴用户data.Its工作的罚款。问题在你的情况似乎是,某些如何标题内容类型是 内容类型:application/schema + json而不是Content-Type:application/json

你可以使用一些其他的休息客户端,而不是卷曲,并通过设置Content-Type:application/json头部来验证。

也正好试试以下方法:

Remove import com.fasterxml.jackson.annotation.JsonView; from User model class. 

而且注释掉以下两个标注在仓库类:

//@PropertySource(value = { "classpath:application.properties" }) 
//@ComponentScan 

从这里你可以查看修改后的运行代码:

https://drive.google.com/file/d/0B-o8NN2qlp3DRVpUS044UmlmRWs/edit?usp=sharing

+0

http://spring.io/guides/gs/accessing-data-rest/ 如果您查看此链接,JPA将为开发人员完成所有映射。对于该链接中给出的示例,他们没有使用POST或GET的任何此类注释。我的代码中只有差异是MYSQL而不是h2数据库。 –

+0

请检查我的修改回答 –

+0

嗨,谢谢你的时间。我删除了导入和@componentscan注释,无法删除属性源,因为它具有mysql相关参数。我仍然面临同样的问题,如果我没有指定的内容比我也面临不同的问题 –

相关问题