Nim*_*sky 91 java spring http spring-mvc
是否有必要包裹背衬物体?我想做这个:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}
Run Code Online (Sandbox Code Playgroud)
并使用这样的JSON:
{
"str1": "test one",
"str2": "two test"
}
Run Code Online (Sandbox Code Playgroud)
但相反,我必须使用:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Holder holder) {}
Run Code Online (Sandbox Code Playgroud)
然后使用这个JSON:
{
"holder": {
"str1": "test one",
"str2": "two test"
}
}
Run Code Online (Sandbox Code Playgroud)
那是对的吗?我的另一个选项是改变RequestMethod
到GET
和使用@RequestParam
的查询字符串,或者使用@PathVariable
与两种RequestMethod
.
Bij*_*men 85
你是对的,@ RequestBody带注释的参数应该保存请求的整个主体并绑定到一个对象,所以你基本上必须使用你的选项.
如果您绝对需要自己的方法,可以使用自定义实现:
说这是你的json:
{
"str1": "test one",
"str2": "two test"
}
Run Code Online (Sandbox Code Playgroud)
你想把它绑定到这两个参数:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
public boolean getTest(String str1, String str2)
Run Code Online (Sandbox Code Playgroud)
首先@JsonArg
使用JSON路径定义自定义注释,例如所需信息的路径:
public boolean getTest(@JsonArg("/str1") String str1, @JsonArg("/str2") String str2)
Run Code Online (Sandbox Code Playgroud)
现在编写一个Custom HandlerMethodArgumentResolver,它使用上面定义的JsonPath来解析实际参数:
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import com.jayway.jsonpath.JsonPath;
public class JsonPathArgumentResolver implements HandlerMethodArgumentResolver{
private static final String JSONBODYATTRIBUTE = "JSON_REQUEST_BODY";
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(JsonArg.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String body = getRequestBody(webRequest);
String val = JsonPath.read(body, parameter.getMethodAnnotation(JsonArg.class).value());
return val;
}
private String getRequestBody(NativeWebRequest webRequest){
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
String jsonBody = (String) servletRequest.getAttribute(JSONBODYATTRIBUTE);
if (jsonBody==null){
try {
String body = IOUtils.toString(servletRequest.getInputStream());
servletRequest.setAttribute(JSONBODYATTRIBUTE, body);
return body;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
现在只需在Spring MVC中注册即可.有点涉及,但这应该干净利落.
Kon*_*ong 71
虽然确实@RequestBody
必须映射到单个对象,但该对象可以是a Map
,因此这为您提供了一个很好的方法来实现您尝试实现的目标(无需编写一个关闭后备对象):
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Map<String, String> json) {
//json.get("str1") == "test one"
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一个完整的JSON树,你也可以绑定到Jackson的ObjectNode:
public boolean getTest(@RequestBody ObjectNode json) {
//json.get("str1").asText() == "test one"
Run Code Online (Sandbox Code Playgroud)
azw*_*bar 15
用于传递多个对象、参数、变量等。您可以使用 jackson 库中的 ObjectNode 作为参数动态执行此操作。你可以这样做:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody ObjectNode objectNode) {
// And then you can call parameters from objectNode
String strOne = objectNode.get("str1").asText();
String strTwo = objectNode.get("str2").asText();
// When you using ObjectNode, you can pas other data such as:
// instance object, array list, nested object, etc.
}
Run Code Online (Sandbox Code Playgroud)
我希望这会有所帮助。
小智 8
您可以通过使用body和path变量来混合post参数,以获得更简单的数据类型:
@RequestMapping(value = "new-trade/portfolio/{portfolioId}", method = RequestMethod.POST)
public ResponseEntity<List<String>> newTrade(@RequestBody Trade trade, @PathVariable long portfolioId) {
...
}
Run Code Online (Sandbox Code Playgroud)
简单的解决方案是创建一个包含 str1 和 str2 作为属性的有效负载类:
@Getter
@Setter
public class ObjHolder{
String str1;
String str2;
}
Run Code Online (Sandbox Code Playgroud)
当你可以通过之后
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody ObjHolder Str) {}
Run Code Online (Sandbox Code Playgroud)
您的请求正文是:
{
"str1": "test one",
"str2": "two test"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
162490 次 |
最近记录: |