2014-07-20 87 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相關參數。我仍然面臨同樣的問題,如果我沒有指定的內容比我也面臨不同的問題 –

相關問題