Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

初学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

image

然后我按照网络的答案

value = "username",required = false
value = "password",required = false

依然无法请求

——————————————————————————

改成新的方法

package com.resume.server;
import com.alibaba.fastjson.JSONObject;
import com.resume.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginController {
    @RequestMapping("/helloworld")
    public String show() {
        return "Hello world";
 }
    @ResponseBody
 @PostMapping("/common/fgadmin/login")
    public String getInteger(@RequestBody JSONObject jsonObject){
        String code=jsonObject.get("code").toString();
 String a=jsonObject.get("id").toString();
 Integer id=Integer.parseInt(a);
 return "数字:"+id+"字符串:"+code;
 }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...