主要問題是我無法使用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
在這裏看起來很可疑的是,你據稱獲得'appl對於所述請求而言,我不能相信,這是我無法相信的。不要認爲這與JPA設置有關,而與傑克遜整合有關。 –
嗨, 感謝您關注此事。 –
嗨, 感謝您關注此事。問題可能在於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類的對象中。 在此先感謝。 –