【java – 为什么@RequestParam注释绑定请求体的值?】教程文章相关的互联网学习教程文章

Java servlet:request.getParameter()从POST请求中的查询字符串返回参数【代码】

我目前正在开发在Glassfish 4下运行的Servlet.我实现了doPost()方法,并且需要确保使用POST正文而不是在查询字符串中传递参数. 我写了一个测试实现来检查它:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String name = request.getParameter("name");response.getOutputStream().print(name); }如果我通过带有该网址的POST调用页面:http://localhost...

java-HK2没有用球衣注入HttpServletRequest【代码】

我试图按照位于here的示例创建工厂以注入我的HttpSession.不幸的是,无论我尝试什么,都无法正常工作.不知道可能是什么问题. 我试过只注入HttpServletRequest和提供程序.这是我使用提供程序的示例.尝试在provide方法中访问提供程序时,该错误是空指针异常.如果我尝试注入HttpServletRequest,那么将无法获取任何对象.我正在使用JerseyTest在GrizzlyTestContainer中运行它.为了绑定HttpServletRequest,我需要添加一些内容吗?我似乎找不...

java-如何为@RequestParam的defaultValue提供表达式【代码】

我有一个使用RequestParam的控制器.像这样@RequestMapping("/hello") public ModelAndView process(@RequestParam(value = "startDate", required = false, defaultValue="yesterday()") startDate)如果未指定startDate,则使用defaultValue.但就我而言,该值应该是动态的,例如now()或昨天().有没有办法指定一个表达式例如类的方法返回值? 我太懒了,无法在任何地方使用这样的代码startDate=startDate!=null ? startDate : new Date(...

java-Spring MVC @RestController->无论我做什么,都会在“ 400 Bad Request”中放入结果【代码】

我有一个Spring RestController,当我从中获取数据时,它工作得很好,但是当我尝试放入非常相同的数据时,我得到了400 Bad Request. 这是我的控制器仍然可以正常工作的最简单版本(我省略了GET方法):@RestController @RequestMapping("/configuration/ledstrips/{name}/display") public class DisplayController {@ResponseBody@RequestMapping(method = RequestMethod.PUT, produces = { "application/hal+json" })public DisplayRes...

coding++:Java 获取request中的参数【代码】

第一种:private Map<String,Object> mapParameters(HttpServletRequest request) {//封装查询条件参数Map<String, Object> map = new HashMap<String, Object>();Enumeration enu = request.getParameterNames();while (enu.hasMoreElements()) {String key = (String) enu.nextElement();String value = request.getParameter(key);map.put(key, value);}return map; }第二种:Map map=request.getParameterMap(); Set keSet=ma...

java-Spring org.springframework.web.multipart.support.MissingServletRequestPartException,必需的请求部分“文件”【代码】

我试图通过在FormBodyPart中使用它来将文件发送到控制器,而不是直接将文件发送到它.这是收集文件的代码private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType)throws ClassificationException {if (inputFile == null) {throw new ClassificationException("Null input file provided");}if (!inputFile.exists()) {throw new ClassificationException("Input file not found: " + inputFile.get...

java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色【代码】

我有一个在Tomcat应用程序服务器上运行并针对第三方IdP进行身份验证的Spring Boot Web应用程序. 目前,我们在许多应用程序中使用< security-role>进行基于角色的身份验证.和< security-constraint>在web.xml中,并且可以正常工作. 现在,尝试使用Spring Security,我添加了以下配置类:@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(final...

java-如何在camera2 API中使用reprocessCaptureRequest【代码】

我正在尝试将相机项目更新为Android N,因此将旧的CameraCaptureSession移至ReprocessableCaptureSession.我做到了,它工作正常,但是有了这个新功能,我可以在我的相机中使用CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG模板设备,我可以使用reprocessCaptureRequest重新处理框架. 这是我的问题出现的地方.因为我找不到任何示例,并且我不太了解有关如何使用reprocessCaptureRequest的小文档:Each reprocess CaptureRequest processes one ...

java-Spring / Rest @RequestBody字符编码

我正在使用环境Tomcat 7,并且已经部署了Spring / Rest我有一个方法是post方法,它通过@RequestBody接受json,其中json中的某些键具有阿拉伯文本.当我将请求发送到上述post方法并解析jsonobject时,我得到的是问号代替阿拉伯语文本.解决方法:您需要接受utf-8编码.如果您正在使用POSTMAN来测试您的API,则可以看到您的应用程序接受为Accept-Encoding. 如果它不是UTF-8,那么您需要在您的应用程序中允许它.

Java-Kotlin中的okhttp3 RequestBody【代码】

我想发送这样的请求正文: 我该如何发送?我将其作为课程发送吗?"email":{"emailto":"foo@bar.com","emailfrom":"foo@bar.com","emailcc":"foo@bar.com","emailbcc":"foo@bar.com","emailsubject":"Subject","emailtag":"Information","emailtextbody":"---","attachments":[] }val formBody = FormBody.Builder().add("email", "Your message").build()val request = Request.Builder().url(url).post(formBody).addHeader("_Tok...

java-Spring Reactive使用ServerRequest获取正文JSONObject【代码】

我是春季反应性新手. 我正在尝试使用邮递员从服务器获取请求信息. 首先,邮递员使用post方法将信息发送到服务器.其次,我们一直在服务器端处理相关代码并获取请求信息. 在以下代码段中 我想知道是否可以获取ServerRequest函数的JSONObject. 邮递员正文(应用程序/ json){"name": "aaaa","name_order": ["aa", "bb", "cc"],"type": "12","query": "" }java(RouterFunction)import com.ntels.io.input.handler.RestInHandler; import or...

java-如何将Map转换为RequestBody?【代码】

使用Retrofit 2.4.0,我正在发出@Multipart @POST请求.我正在将文件作为@Part以及一些元数据作为@PartMap发送.这就是通话的样子.@Multipart @POST("https://8hoot.com/my-path") Single<Response<UploadMediaResponseModel>> uploadMedia(@PartMap Map<String, RequestBody> metadata,@Part MultipartBody.Part filePart );还有另一个Map< String,String&gt ;,我们称之为subMetaMap,其中包含相关的键值对.如何将该subMetaMap存储在@...

java实现类似python requests的session功能

怎么在java中实现类似于python的requests模块的session功能呢。java也是可以实现的,用java的okhttp包可以实现。 在pom.xml中加入相关依赖<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.2.2</version> </dependency>后面是相关的java代码:package com.puple.atto.datastructure;import java.text.Mes...

java-使用@RequestBody将JSON转换为Enum类型【代码】

我有一个主枚举类,它本质上是一种对象类型的类定义.例如,它看起来像下面的示例:public enum ColorDefinition {private String abbrev;private String color;private Class colorClass;RED("RD", "Red", Red.class),GREEN("GN", "Green", Green.class),BLUE("BL", "Blue", Blue.class).... }我正在尝试从Javascript模型设置发布请求,该请求在正文中发送映射,例如{红色:255,绿色:0,蓝色:0}到使用@RequestMapping(value = "v1/col...

java-如何在Tomcat的Jaas登录模块中访问HttpServletRequest【代码】

我们有一项任务是在已通过Websphere认证并正在运行的Tomcat7中认证我们的应用程序. 我们在Websphere中的应用程序具有JAAS定制登录模块,它通过回调处理程序访问HttpServletRequest对象,如下所示,并且我们在会话对象中设置了与用户相关的信息(可从请求中检索).javax.security.auth.callback.Callback callbacks[] = new javax.security.auth.callback.Callback[3]; callbacks[0] = new javax.security.auth.callback.NameCallback("U...