gpt4 book ai didi

java - 将嵌套的 Json 发布到 spring Controller

转载 作者:行者123 更新时间:2023-11-29 03:30:31 25 4
gpt4 key购买 nike

好吧,我正在尝试在 spring Controller 中检索嵌套的 json 并收到 400(错误请求)错误。

JSON

{"AuthenticationInfo":
{"loginId":"243324","password":"xyz"}
}

Controller

  @RequestMapping(value = "/login", method = RequestMethod.POST,headers={"Accept=*/*","content-type=application/json"})
@ResponseBody
public MySubscriber getSubscriber(@RequestBody MyAuthentication myAuthentication) {
LOGGER.log(Level.INFO, "getSubscriber");

System.out.println("getSubscriber method : "+myAuthentication);


MySubscriber mySubscriber = helloWebService.getSubscriber(myAuthentication);
LOGGER.log(Level.INFO, "mySubscriber : " + mySubscriber);
System.out.println( "mySubscriber : " + mySubscriber);
return mySubscriber;
}

我的身份验证

public class MyAuthentication extends AuthenticationInfo {
private AuthenticationInfo AuthenticationInfo;

public AuthenticationInfo getAuthenticationInfo() {
return AuthenticationInfo;
}

public void setAuthenticationInfo(AuthenticationInfo authenticationInfo) {
AuthenticationInfo = authenticationInfo;
}

@Override
public String toString()
{
return "AuthenticationInfo : "+AuthenticationInfo;
}
}

认证信息

    public class AuthenticationInfo {
private String loginId;
private String password;
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString()
{
return "{ loginId : "+loginId+" || password"+password+"}";
}
}

当我触发简单的 Json 并相应地检索它时,错误就出现了。这里唯一的问题是 Json 的嵌套结构

最佳答案

尝试像这样修改MyAuthentication

    public static class MyAuthentication extends AuthenticationInfo {
@JsonProperty("AuthenticationInfo")
private AuthenticationInfo AuthenticationInfo;

public IndexController.AuthenticationInfo getAuthenticationInfo() {
return AuthenticationInfo;
}

public void setAuthenticationInfo(IndexController.AuthenticationInfo authenticationInfo) {
AuthenticationInfo = authenticationInfo;
}

@Override
public String toString() {
return "AuthenticationInfo : " + AuthenticationInfo;
}
}

Jackson 的默认 jsoin 属性以小写字母开头。

关于java - 将嵌套的 Json 发布到 spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com