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
然后我按照网络的答案
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…