使用带有Post请求的HTTP dev客户端和Content-Type application/x-www-form-urlencoded
1)只有@RequestBody
请求 - localhost:8080/SpringMVC/welcome In Body - name = abc
码-
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, Model model) {
model.addAttribute("message", body);
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
//按预期将body标记为"name = abc"
2)只有@RequestParam
请求 - localhost:8080/SpringMVC/welcome In Body - name = abc
码-
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestParam String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
//按预期将名称命名为"abc"
3)两者在一起
请求 - localhost:8080/SpringMVC/welcome In Body - name = abc
码-
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, …Run Code Online (Sandbox Code Playgroud) 我已经通过Spring文档了解@RequestBody,并且他们给出了以下解释:
所述
@RequestBody方法参数注释指示方法参数应绑定到HTTP请求正文的值.例如:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用a将请求主体转换为方法参数
HttpMessageConverter.HttpMessageConverter负责从HTTP请求消息转换为对象并从对象转换为HTTP响应主体.
DispatcherServlet支持使用DefaultAnnotationHandlerMapping和支持基于注释的处理AnnotationMethodHandlerAdapter.在Spring 3.0中,AnnotationMethodHandlerAdapter扩展为支持@RequestBody并HttpMessageConverter默认注册以下s:...
但我的困惑是他们在文档中写的句子
@RequestBody方法参数注释指示应将方法参数绑定到HTTP请求正文的值.
那是什么意思?任何人都可以举个例子吗?
@RequestParamspring doc中的定义是
注释,指示应将方法参数绑定到Web请求参数.支持带注释的处理程序方法
Servlet和Portlet环境.
我在他们之间感到困惑.请帮助我举一个关于它们彼此不同的例子.
spring spring-mvc servlet-3.0 http-request-parameters spring-boot
我已经阅读了如何使用JSF发送参数但是如果用户companyId在访问其登录页面时在URL中键入它们会怎么样?例如,
http://my.company.url/productName/login.faces?companyId = acme.
我们现在的方式是,有一些scriptlet代码可以从请求中获取值,然后在会话中设置它.该参数从登录页面开始改变其外观,因此每个客户可以具有不同的登录页面视图.在切换到JSF之前,我们正在使用extj.
有没有办法使用JSF 2或PrimeFaces?
为什么Spring 3.2只根据requestparam为"0"或"1"来映射我的布尔值?
@RequestParam(required= false, defaultValue = "false") Boolean preview
Run Code Online (Sandbox Code Playgroud)
预览只会"true"在请求参数"?preview=1"很奇怪的时候进行
我想要它"?preview=true".我怎么做?
我有3页:
main.xhtmlagreement.xhtmlgenerated.xhtml将agreement.xhtml需要两个参数正确加载:serviceId和site.所以,正常的网址看起来像这样:/app/agreement.xhtml?site=US&serviceId=AABBCC.
我有这个按钮 agreement.xhtml
<h:form>
<h:commandButton value="Generate License File" action="#{agreement.generateMethod}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
该@RequestScoped豆#{agreement}有这样的方法:
public String generateMethod(){
.......
return "generated";
}
Run Code Online (Sandbox Code Playgroud)
我需要,在点击时,generateMethod()方法被执行,完成后,用户被重定向到generated.xhtml页面.发生了什么事是,在点击,网页浏览器发送的用户/app/agreement.xhtml,而且由于它不发送参数site和serviceId,它崩溃.
我尝试过generateMethod()返回a "generated?faces-redirect=true",但仍然没有.有任何想法吗?
目前,我们正在上一个非常简单的webapp,我们想"模糊" (这将是正确的术语?),或者以某种方式编码的请求参数,所以我们可以减少从机会发送任意数据空闲用户.
例如,网址看起来像 /webapp?user=Oscar&count=3
我们希望有类似的东西:/webapp?data=EDZhjgzzkjhGZKJHGZIUYZT并在服务器中使用真实的请求信息解码该值.
在开始实现这样的事情之前(并且可能做错了)我想知道是否有什么事情要做?
我们在服务器上有Java,在客户端上有JavaScript.
javascript java obfuscation web-applications http-request-parameters
HTTPServletRequestreq,有一个方法,getParameterMap()但是,对于post数据,值返回a String[]而不是String
名称=结婚&lastName的=约翰&年龄= 20.
我在post数据中看到它不是一个数组,但是getParameterMap()为每个键(name或lastName或Age)返回数组.有没有更好地理解这一点的指示?
方法2中提供了代码.方法1完全正常.
方法1:
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String key = (String) parameterNames.nextElement();
String val = req.getParameter(key);
System.out.println("A= <" + key + "> Value<" + val + ">");
}
Run Code Online (Sandbox Code Playgroud)
方法2:
Map<String, Object> allMap = req.getParameterMap();
for (String key : allMap.keySet()) {
String[] strArr = (String[]) allMap.get(key);
for (String val : strArr) {
System.out.println("Str Array= " + val);
}
}
Run Code Online (Sandbox Code Playgroud) 我想验证我的控制器中的请求参数之一。请求参数应该来自给定值列表之一,如果不是,则应该抛出错误。在下面的代码中,我希望请求参数 orderBy 来自@ValuesAllowed 中存在的值列表。
@RestController
@RequestMapping("/api/opportunity")
@Api(value = "Opportunity APIs")
@ValuesAllowed(propName = "orderBy", values = { "OpportunityCount", "OpportunityPublishedCount", "ApplicationCount",
"ApplicationsApprovedCount" })
public class OpportunityController {
@GetMapping("/vendors/list")
@ApiOperation(value = "Get all vendors")
public ResultWrapperDTO getVendorpage(@RequestParam(required = false) String term,
@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size,
@RequestParam(required = false) String orderBy, @RequestParam(required = false) String sortDir) {
Run Code Online (Sandbox Code Playgroud)
我编写了一个自定义 bean 验证器,但不知何故这不起作用。即使我为查询 param 传递了任何随机值,它也不会验证并抛出错误。
@Repeatable(ValuesAllowedMultiple.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {ValuesAllowedValidator.class})
public @interface ValuesAllowed {
String message() default "Field value should …Run Code Online (Sandbox Code Playgroud) validation controller spring-annotations http-request-parameters spring-boot
我用来@RequestParam获取参数值,但我发现如果我传递像“name=abc&def&id=123”这样的值,我将得到名称值“abc”而不是“abc&def”。我发现对参数值进行编码和解码可以解决我的问题。但是我必须在每个控制器方法中编写编码和解码方法,spring有解码每个 @RequestParam值的全局方法吗?使用时@RequestParam,是否需要编码和解码每个值?
这是我的代码:
@PostMapping("/getStudent")
public Student getStudent(
@RequestParam String name,
@RequestParam String id) {
name= URLDecoder.decode(name, "UTF-8");
//searchStudent
return Student;
}
@PostMapping("/getTeacher")
public teacher getTeacher(
@RequestParam String name,
@RequestParam String teacherNo) {
name= URLDecoder.decode(name, "UTF-8");
//searchTeacher
return teacher;
}
Run Code Online (Sandbox Code Playgroud)
有人说Spring已经这样做了,但是我尝试过,结果不对。只使用curl cmd可以,但是java代码不行。
@PostMapping(value = "/example")
public String handleUrlDecode1(@RequestParam String param) {
//print ello%26test
System.out.println("/example?param received: " + param);
return "success";
}
@GetMapping(value = "/request")
public String request() {
String url = "http://127.0.0.1:8080/example?param=ello%26test";
System.out.println(url);
RestTemplate restTemplate = …Run Code Online (Sandbox Code Playgroud) 我需要发送一个HTTP请求,我可以这样做,但我在Backendless中的API需要HTTP请求标头中的application-id和secret-key.你能帮忙把它添加到我的代码中吗?谢谢
let urlString = "https://api.backendless.com/v1/data/Pub"
let session = NSURLSession.sharedSession()
let url = NSURL(string: urlString)!
session.dataTaskWithURL(url){(data: NSData?,response: NSURLResponse?, error: NSError?) -> Void in
if let responseData = data
{
do{
let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments)
print(json)
}catch{
print("Could not serialize")
}
}
}.resume()
Run Code Online (Sandbox Code Playgroud) http http-headers http-request http-request-parameters swift
spring ×4
spring-boot ×3
java ×2
jsf ×2
jsf-2 ×2
spring-mvc ×2
boolean ×1
controller ×1
http ×1
http-headers ×1
http-post ×1
http-request ×1
javascript ×1
managed-bean ×1
navigation ×1
obfuscation ×1
post ×1
query-string ×1
request ×1
servlet-3.0 ×1
servlets ×1
swift ×1
validation ×1