如何使请求体控制器工作?



所以实际上一切正常,除了我无法访问"inscrit",它应该是发送给信息用户的那个

这是我在javascript中的函数,它确实在ajax上取得了成功。

function fbLogin() {
FB.login(function (response) {
if (response.authResponse) {
var inscrit = {
"prenom": response.first_name,
"nom": response.last_name,
"email": response.email
}
$.ajax({
method: 'post',
url: '/hello/facebookconnection',
contentType : 'application/json; charset=utf-8',
data: JSON.stringify(inscrit),
datatype: "json",
success: function() {
window.alert('User data sent');}
,
error: function(){
window.alert('Error in sending ajax data');}
});
} else {
document.getElementById('status').innerHTML = 'User cancelled login or did not fully authorize.';
}
}, {scope: 'email'});
}

这是我的java控制器(无论我做什么,"inscrit.getEmail()"总是空的)

@RequestMapping(value = "/facebookconnection", method = RequestMethod.POST)
@ResponseBody public void createUser(@RequestBody inscrit inscrit, ModelMap model) throws Exception {
//System.out.println(inscrit.getNom());
//System.out.println(inscrit.getPrenom());
System.out.println(inscrit.getEmail());
try {
user.adduser(inscrit);
} catch (Exception e) {
e.printStackTrace();
}
}

这是我的 incrit 类,所有 getter 和 setter 都正确设置(我知道它没有问题)

@Entity
@Table(name="inscrit")
public class inscrit implements Serializable {
@Id
@Column(name="email")
private String email;
@Column(name="nom")
private String nom;
@Column(name="prenom")
private String prenom;
}

所以我想知道是否有人可以帮助我理解为什么它在我的控制器中不起作用(如何做到这一点,数据从 ajax 正确发送到控制器):( 感谢!

编辑:我确实在下面写了这个问题的答案。问题是response.first_name真的是空白的......并且必须通过具有真实信息的函数 userdata()。所以这意味着我的代码中没有任何真正的错误,控制器和我的 jsp 页面之间的所有信息都可以正常工作。自由使用它作为其工作原理的示例

@ResponseBody public void createUser(@RequestParam(value="inscrit") String inscrit) throws Exception {
//System.out.println(inscrit.getNom());
//System.out.println(inscrit.getPrenom());
Inscrit ins = JSON.parseObject(inscrit, Inscrit.class);
System.out.println(ins.getEmail());    
try {
user.adduser(ins);
} catch (Exception e) {
e.printStackTrace();
}  }

incrit.java类是非标准的。您应该将其更改为Inscrit爪哇

我希望这有所帮助。

自从我找到答案以来,我将回答我自己的问题,因此,这适用于尝试拥有 facebook API 并希望获取要放入数据库的信息的人。 我的错误是我认为登录中的响应与UserData()相同,事实并非如此...这就是我应该这样做的方式。

function fbLogin() {
FB.login(function (response) {
if (response.authResponse) {
// Get and display the user profile data
getFbUserData();
} else {
document.getElementById('status').innerHTML = 'User cancelled login or did not fully authorize.';
}
}, {scope: 'email'});
}
// Fetch the user profile data from facebook
function getFbUserData(){
FB.api('/me', {locale: 'en_US', fields: 'id,first_name,last_name,email,link,gender,locale,picture'},
function (response) {
/*document.getElementById('fbLink').setAttribute("onclick","fbLogout()");
document.getElementById('fbLink').innerHTML = 'Logout from Facebook';
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.first_name + '!';
document.getElementById('userData').innerHTML = '<div class="fbprofile"><p><b>FB ID:</b> '+response.id+'</p><p><b>Name:</b> '+response.first_name+' '+response.last_name+'</p><p><b>Email:</b> '+response.email+'</p><p><b>Gender:</b> '+response.gender+'</p><p><b>Locale:</b> '+response.locale+'</p></div><p><b>Picture:</b> <img src="'+response.picture.data.url+'"/></p>';*/
var inscrit = {
"prenom": response.first_name,
"nom": response.last_name,
"password": "1234",
"status": 0,
"email": response.email
}
$.ajax({
method: 'post',
url: '/hello/facebookconnection',
contentType : 'application/json; charset=utf-8',
data: JSON.stringify(inscrit),
datatype: "json",
success: function() {
document.location.href = "accueilmember";
},
error: function(){
window.alert('Error in sending ajax data');}
});
});
}

这是我的控制器类。这只会记录发送到服务器的 post 请求:(如果您需要更多类似于添加响应正文的内容,您可以修改它)

@RestController
public class EmailController {      
@RequestMapping(value = "/facebookconnection", method = RequestMethod.POST)
public void createUser(@RequestBody inscrit inscrit) throws Exception {
System.out.println(inscrit.getNom());
System.out.println(inscrit.getPrenom());
System.out.println(inscrit.getEmail());

}

这是我的inscrit.java类(我个人认为命名类不会影响,但最好遵循标准命名实践,如Inscrit.java)

import java.io.Serializable;
public class inscrit implements Serializable {
private static final long serialVersionUID = -8445943548965154778L;
private String email;
private String prenom;
private String nom;
public inscrit() {
super();
}
public inscrit(String email,String prenom,String nom) {
this.setEmail(email);
this.setPrenom(prenom);
this.setNom(nom);
}
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public String getPrenom() {
return prenom;
}

public void setPrenom(String prenom) {
this.prenom = prenom;
}

}

我从高级 Rest 客户端发出了发布请求,并将有效负载设置为

{
"email":"agdfae@gmail.com",
"prenom":"qewrwe",
"nom":"qwer"
}

结果是

qwer
qewrwe
agdfae@gmail.com

您可以修改此设置以满足自己的需求。希望这有帮助

编辑:

在 Spring 引导中,您将需要更多两个类,如下所示:

ServletInitializer.java

public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PostDataApplication.class);
}
}

后数据应用程序.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PostDataApplication {
public static void main(String[] args) {
SpringApplication.run(PostDataApplication.class, args);
}
}

PostDataApplication 是应用程序的名称。

这四个类可以解决问题(使用 pom.xml只是不需要其他 xml 配置,因为这是 springboot)。如果您有任何问题,请告诉我。

最新更新