初学SpringBoot,使用post请求String parameter 'username' is not present

Application.java

package com.resume;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

@ComponentScan("com.resume")

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

LoginController.java

package com.resume.server;

import com.resume.bean.User;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import net.sf.json.JsonConfig;

import net.sf.json.util.PropertyFilter;

@RestController

public class LoginController {

@RequestMapping("/helloworld")

public String show() {

return "Hello world";

}

@ResponseBody

@RequestMapping(value="/common/fgadmin/login",method=RequestMethod.POST,produces="application/json;charset=UTF-8")

public JSONObject getByJSON( @RequestParam(value = "username",required = false) String username,

@RequestParam(value = "password",required = false) String password,HttpServletResponse response) {

JSONObject result=new JSONObject();

if(username.equals("abc")&&password.equals("123")) {

Cookie cookie = new Cookie("login","true");

response.addCookie(cookie);

result.element("message","success");

result.element("code","200");

}

else {

result.element("message","fail");

result.element("code","400");

}

return result;

}

}

请求接口http://127.0.0.1:8033/common/fgadmin/login

初学SpringBoot,使用post请求String parameter 'username' is not present

然后我按照网络的答案

value = "username",required = false

value = "password",required = false

依然无法请求

回答

好好看看教程和文档,Json 的 body,用 @RequestBody + 类接
你这里 @RequestBody JsonObject request 放参数里就行了
顺便一说,@RequestMapping里的那个produce字段可以去掉
顺便一说,JsonObject这套API拿来玩玩可以,真用来做业务是要命的

以上是 初学SpringBoot,使用post请求String parameter 'username' is not present 的全部内容, 来源链接: utcz.com/a/84123.html

回到顶部