首页 > java后台的接口,没有指明用post还是get,就意味着都可以吗?

java后台的接口,没有指明用post还是get,就意味着都可以吗?


@Controller
public class ClinicController {
    private final Clinic clinic;

    @Autowired
    public ClinicController(Clinic clinic) {
        this.clinic = clinic;
    }

    @RequestMapping("/")
    public void welcomeHandler() {
    }

    @RequestMapping("/vets")
    public ModelMap vetsHandler() {
        return new ModelMap(this.clinic.getVets());
    }
}

以上代码没有指定请求必须是GET方法还是PUT/POST或其他方法,@RequestMapping注解默认会映射所有的HTTP请求方法。如果仅想接收某种请求方法,请在注解中指定之@RequestMapping(method=GET)以缩小范围。

这是Spring MVC中关于@RequestMapping的说明文档,多看文档大兄弟。


这是用的spring mvc?
没有指明RequestMethod方法,则符合RequestMapping规则的get,Post都可以.不过从方法名来看,这里应该是使用get方法比较好.
特例是
如果存在同规则RequestMapping的话,同名RequestMapping指明了单个RequestMethod方法,则它的优先级会高


是滴,不写的话,默认支持所有HTTP请求方法,见如下参考文档


嗯,但是讲道理用get比较好
ps:不是很懂踩我的人的心态


是的,请参考HttpServlet类中的doGet,doPost,Serivce三个方法,springMVC的封装底层依然不能离开这三个方法。


可以指定请求方式:connection.setRequestMethod("POST");如下代码

public static String httpPostWithJson(String ecUrl, String params) {
        BufferedReader reader = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(ecUrl);
            connection = (HttpURLConnection) url.openConnection();
            // 创建连接
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.connect();

            // POST请求
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(params);
            out.flush();
            out.close();

            // 读取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            return sb.toString();
        } catch (MalformedURLException e) {
            logger.error("httpPostWithJsonMalformedURLException error", e);
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            logger.error("httpPostWithJsonUnsupportedEncodingException error", e);
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("httpPostWithJsonIOException error", e);
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
                if (null != connection) {
                    connection.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

是的,需要限制的话用RequestMethod指定下


那就用 get 吧!

【热门文章】
【热门文章】