我有一个带有以下控制器的Spring应用程序:
urlParams
然而,当我从邮递员发送包含以下内容的POST请求时
auxInstruction: Object //<- parent instructions
child: null //<- child instructions
component: ComponentInstruction //<-current component object
specificity: 10000
urlParams: Array[0] <-- parameter passed for the route
urlPath: "about" //<-- current url path
我收到以下回复:
@RestController
@RequestMapping("/app")
public class RegisterRestController {
@Autowired
UserRepository userRepository;
@Autowired
PasswordEncoder passwordEncoder;
@Autowired
UserService userService;
@RequestMapping( value="/loginuser", method =RequestMethod.POST,produces="application/json")
public String loginUser(@RequestBody String requestBody) {
System.out.println("inside");
JSONObject responseJsonObject = new JSONObject();
String phonenumber;
String password;
try{
JSONObject object = new JSONObject(requestBody);
phonenumber = object.getString("phonenumber");
password = object.getString("password");
User user = userService.findByNumber(phonenumber);
String sha256Password = passwordEncoder.encode(password);
if(sha256Password.equals(user.getPassword())){
responseJsonObject.put("response", "Login Successful");
}
else {
responseJsonObject.put("repsonse", "Login failed");
}
}
catch (Exception e){
e.printStackTrace();
try {
responseJsonObject.put("response", "Invalid Credentials");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return responseJsonObject.toString();
}
另外,我也在试验Spring Security。服务器没有显示任何错误,控制器似乎没有收到请求,因为&#34;内部&#34;没有打印。我试图了解Spring,但是我找不到出现这种错误的原因。我将不胜感激任何帮助。提前致谢
答案 0 :(得分:8)
您的代码中存在两个问题:
@RequestBody String requestBody
您正在发送一个包含两个属性的对象:
{
"phonenumber": "9123456789",
"password": "password"
}
解决方案:
为您需要登录的值创建一个类:
public class Login {
public String phonenumber;
public String password;
// you need a zero argument constructor
// maybe you have to add getter and setters
}
更改您的控制器方法,以便它需要此类型的对象
@RequestBody Login requestBody
答案 1 :(得分:1)
Jackson库将使用您在登录用户方法中定义的构造函数自动转换为JSON。所以你不需要转换为json。所以这意味着
{
"phonenumber": "9123456789",
"password": "password"
}
应该在你的构造函数中定义。您应该已经定义了一个定义loginUser的实体类。
public class LoginUser{
String phonenumber;
String password;
// define all other variables needed.
public LoginUser(String phonenumber, String password){
this.phonenumber = phonenumber ;
this.password = password;
}
public LoginUser() {
//you need a default contructor. As srequired by spring
}
//Define the gettters and settters
}
然后
@RequestMapping( value="/loginuser", method = RequestMethod.POST,produces="application/json")
public String loginUser(@RequestBody LoginUser requestBody) {
System.out.println("inside");
try{
phonenumber = requestBody.getPhonenumber; // please define your getters and setters in the login class
password = requestBody.getpassword;
User user = userService.findByNumber(phonenumber);
String sha256Password = passwordEncoder.encode(password);
if(sha256Password.equals(user.getPassword())){
responseJsonObject.put("response", "Login Successful");
}
else {
responseJsonObject.put("repsonse", "Login failed");
}
}
catch (Exception e){
e.printStackTrace();
try {
responseJsonObject.put("response", "Invalid Credentials");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return responseJsonObject.toString();
}
您可以使用邮递员立即尝试。古德勒克
答案 2 :(得分:0)