我试图传递路径参数和查询URL中的参数,但我得到一个奇怪的错误.下面是代码
String url = "http://test.com/Services/rest/{id}/Identifier"
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1234");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
.queryParam("name", "myName");
String uriBuilder = builder.build().encode().toUriString();
restTemplate.exchange(uriBuilder , HttpMethod.PUT, requestEntity,
class_p, params);
Run Code Online (Sandbox Code Playgroud)
我的网址正在变成 http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName
我该怎么做才能让它发挥作用.我期待http://test.com/Services/rest/{id}/Identifier?name=myNameparams将id添加到url
请建议.提前致谢
java query-parameters url-parameters resttemplate path-parameter
据我所知,两者都有同样的目的.除了@PathVariable来自Spring MVC并@PathParam来自JAX-RS 的事实.有什么见解吗?
servlet是否支持URL如下:
/xyz/{value}/test
Run Code Online (Sandbox Code Playgroud)
其中的价值可以用文字或数字代替.
如何在web.xml中映射?
我正在开发一个带有路由和路径参数的角度应用程序。单击按钮即可调用一个函数,将用户重定向到具有相同路径参数的新 URL。虽然浏览器中的URL发生变化,但app-routing.module.ts中定义的相应组件并未加载。如果我再次刷新页面,则会呈现正确的页面。
这个想法是,学生获得一个带有哈希标记的唯一 URL(例如/student/33192c29aa8e449e94f3a1c4eef43ca8),这向他展示了一些指导原则。然后点击一下,他应该被转发到具有相同令牌的注册页面(例如/student/registration/33192c29aa8e449e94f3a1c4eef43ca8)
应用程序路由.module.ts
const routes: Routes = [
...
{ path: 'student/registration/:token', component: RegistrationsComponent },
{ path: 'student/:token', component: GuidelinesComponent },
...
];
Run Code Online (Sandbox Code Playgroud)
指南.component.html
forwardToRegistration(): void {
this.router.navigateByUrl('/student/registration/' + this.studentToken);
}
Run Code Online (Sandbox Code Playgroud)
URL 在浏览器中正确更新,但 RegistrationComponent 未呈现。
感谢您的帮助!
我认为使用REST Web服务的一个主要特征和原因是使用路径参数而不是查询参数.但是许多公开可用的REST Web服务使用查询参数.
我认为查询参数不应该在REST Web服务中使用我错了吗?是否有关于不在REST Web服务中使用查询参数的建议或规则?
有一个@WebServlet(urlPatterns = "/myServlet/").如果用户去myapp/myServlet/other,我仍然希望我的servlet能够捕获.所以说,在servlet路径之后的通配符.我怎么能这样做?
我想用这种模式调用我的Webservice:
/resource/1,2,3
Run Code Online (Sandbox Code Playgroud)
在我的类中,我想将我的参数绑定到对象列表
@Path("/resource")
public class AppWS {
@GET
@Path("/{params}")
public Response get(@PathParam("params") List<MyObject> params) {
return Response.status(200).entity("output").build();
}
}
Run Code Online (Sandbox Code Playgroud)
使用简单的对象:
public class MyObject {
Integer value;
public MyObject(Integer value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
nb:如果有可能我不想创建一个扩展List的MyObjectList(并且有一个分割我的字符串的构造函数)
我该怎么办?
我们正在构建一个为员工数据服务的宁静服务。
我们有一个 api,它将返回属于客户特定部门的员工列表。
这个api需要2个参数——clientId和departmentId
根据标准,以下哪一项是为这个 api 构建一个安静的 url 的正确方法?
1) /client/{clientId}/department/{departmentId}/employees
2) /client/{clientId}/employees?departmentId={departmentId}
一个安静的 url 可以有多个路径参数吗?如果上述问题是/否 - 为什么会这样?
我想为我的 Vert.x Web 驱动的 API 创建一个 URL 结构,它清楚地说明某些实体如何“包含”在其他实体中以及如何“遍历实体路径”以查找子实体,所以我正在考虑使用像这样的事情来解决“孙子”(我不期望比孙子更深的东西):
GET /entity/:parent_id/sub-entity/:child_id/subsub-entity/:grandchild_id
Run Code Online (Sandbox Code Playgroud)
所以通常我的Router配置看起来像这样:
router.get("/entity/:parent_id/sub-entity/:child_id/subsub-entity/:grandchild_id")
.handler(r -> {
HttpServerRequest req = r.request();
Minipart p = Entities.get(req.getParameter("parent_id"))
.getPart(req.getParameter("child_id"))
.getMinipart(req.getParameter("grandchild_id"));
// do something with p
});
Run Code Online (Sandbox Code Playgroud)
当我添加很多操作(每个级别的每个实体类都有目录和创建操作,每个级别的实体实例都有获取、更新和删除操作,以及其他一些花絮)时,我的路由器类变得非常大。
我正在考虑使用子路由器来卸载子实体管理,因此Entities Router配置可能会:
router.mountSubRouter("/entity/:parent_id/sub-entity", PartsRouter.init(vertx));
Run Code Online (Sandbox Code Playgroud)
然后PartsRouter可以这样做:
router.get("/:child_id").handler(r -> {
String parentEntityId = r.request().getParameter("parent_id");
Entity parent = Entities.get(parentEntityId);
String myid = r.request().getParameter("child_id");
Part myself = parent.getPart(myid);
// do something with myself
});
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这样做并尝试访问子路由器操作时,我收到来自 Vert.x 的 404 错误...
更新:
显然 Vert.x 明确不支持这一点 - 它抛出了一个异常,我的包装器代码刚刚记录并忽略了它,说:
java.lang.IllegalArgumentException: …Run Code Online (Sandbox Code Playgroud) 对于带有路径参数的给定路线(下面的示例)
router.GET("/employee/:id", empHandler.GetEmployee)
Run Code Online (Sandbox Code Playgroud)
当尝试使用包含正斜杠的 id path-param(encoded) 调用 url 时
ID = 21/管理员/527
url 编码 ID = 21%2Fadmin%2F527
https://localhost:8000/emplayee/21%2Fadmin%2F527
Run Code Online (Sandbox Code Playgroud)
当我尝试点击此请求时,我收到 404 似乎 gin 会自动解码路径参数并形成一条包含解码路径参数的 url 的路由
https://localhost:8000/emplayee/21/admin/527
Run Code Online (Sandbox Code Playgroud)
我想要员工 ID 路径参数的确切编码值,因为它用于调用需要对其进行 url 编码的其他 api。
path-parameter ×10
java ×3
jax-rs ×2
restful-url ×2
routes ×2
servlets ×2
url ×2
angular ×1
get ×1
go ×1
go-gin ×1
jersey ×1
jersey-2.0 ×1
redirect ×1
rest ×1
resttemplate ×1
spring-mvc ×1
url-pattern ×1
url-routing ×1
urlencode ×1
vert.x ×1
web-services ×1
web.xml ×1
wildcard ×1