我有一个控制器,提供对信息的RESTful访问:
@RequestMapping(method = RequestMethod.GET, value = Routes.BLAH_GET + "/{blahName}")
public ModelAndView getBlah(@PathVariable String blahName, HttpServletRequest request,
HttpServletResponse response) {
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,如果我使用带有特殊字符的路径变量命中服务器,它将被截断.例如: http:// localhost:8080/blah-server/blah/get/blah2010.08.19-02:25:47
参数blahName将是blah2010.08
但是,对request.getRequestURI()的调用包含传入的所有信息.
知道如何防止Spring截断@PathVariable吗?
当与Spring MVC和JSON一起使用时,我一直收到AJAX请求的HttpMediaTypeNotAcceptableException错误.错误的完整堆栈跟踪是..
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:1032)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:972)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:921)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:756)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Run Code Online (Sandbox Code Playgroud)
我做的小google搜索显示请求应该包含类似"accept:application/json"的东西,它确实有..这里是来自firebug的请求标头..
Response Headers
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 2503
Date Thu, 25 Aug 2011 21:00:05 GMT
Connection close
Request Headers
Host localhost:8080
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.20) Gecko/20110803 Firefox/3.6.20 (.NET CLR 3.5.30729)
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost:8080/legaldirectory/index.html
Cookie JSESSIONID=5C97DA19AED4D5FA17F4A58470FAA93B
Run Code Online (Sandbox Code Playgroud)
现在我完全迷失在这里发生的事情......这里还有什么可能出错来解决这个错误......
我正在使用Spring REST和hibernate创建一个Web应用程序.这里我使用来自url的唯一用户名从数据库中获取记录.但问题是,如果我正在编写简单的字符串,那么它工作正常,但在用户名时我正在写点(.)然后没有结果来自数据库.
对于前者
http://localhost:8080/WhoToSubscribe/subscribe/anshul007
Run Code Online (Sandbox Code Playgroud)
但是当我使用这个网址时
http://localhost:8080/WhoToSubscribe/subscribe/nadeem.ahmad095
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为它包含点(.)
这是我的控制器
@RequestMapping(value = "/{uname}", method = RequestMethod.GET)
public @ResponseBody
List<Profession> getSubscriber(@PathVariable("uname") String uname) {
List<Profession> pro = null;
try {
pro = subscribeService.getProfessionById(uname);
} catch (Exception e) {
e.printStackTrace();
}
return pro;
}
Run Code Online (Sandbox Code Playgroud)
这是我的DAO课程
@SuppressWarnings("unchecked")
public List<Profession> getProfessionById(String uname) throws Exception {
session = sessionFactory.openSession();
session.beginTransaction();
String queryString = "from Profession where username = :uname";
Query query = session.createQuery(queryString);
query.setString("uname", uname);
//List<Profession> queryResult = (List<Profession>) query.uniqueResult();
session.getTransaction().commit();
return query.list();
}
Run Code Online (Sandbox Code Playgroud) 我有一个基于RESTful spring的端点,可以将存储在db中的资源存储到javascript编辑器中.相关部分归结为:
@RestController
@RequestMapping(ThemeEndpoint.ENDPOINT_NAME)
public class ThemeEndpoint {
public static final String ENDPOINT_NAME = "/themes";
@RequestMapping(value="/{id}/css/{assetName:.*}", method=RequestMethod.GET)
public Asset getCssItem(
@PathVariable("id") ThemeId id,
@PathVariable("assetName") String name) {
CssThemeAsset themeAsset = themeService.getCssAsset(
id, ThemeAssetId.fromString(name));
Asset asset = new Asset();
asset.name = themeAsset.getName();
asset.text = themeAsset.getContent();
return asset;
}
Run Code Online (Sandbox Code Playgroud)
对于像这样的网址,这可以正常工作
http://localhost:8080/app-url/rest/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.less
Run Code Online (Sandbox Code Playgroud)
但是一旦我将扩展名更改为,就会失败.css.
经过一些调试后,我非常确定如果我使用的是url,请求甚至都没有映射
http://localhost:8080/app-url/rest/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.css
Run Code Online (Sandbox Code Playgroud)
对于高日志级别,我可以看到映射是由spring捕获的:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
- Mapped "{[/themes/{id}/css/{assetName:.*}],methods=[GET],params=[],headers=[],
consumes=[],produces=[application/json],custom=[]}"
onto public xxx.endpoint.ThemeEndpoint$Asset
xxx.endpoint.ThemeEndpoint.getCssItem(
net.lacho.svc.themes.api.ThemeId,java.lang.String)
Run Code Online (Sandbox Code Playgroud)
并且使用非.css扩展名调用控制器:
Found 1 matching mapping(s) for [/themes/ac18a080-a2f1-11e3-84f4-600308a0bd14/css/main.less]
: [{[/themes/{id}/css/{assetName:.*}],methods=[GET],params=[],headers=[],
consumes=[],produces=[application/json],custom=[]}]
Run Code Online (Sandbox Code Playgroud)
但是只要我使用.css作为扩展名 - bang:
Looking up …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring REST 控制器:
@RestController
@RequestMapping(value = "/myresource")
public class MyResourceController {
...
}
Run Code Online (Sandbox Code Playgroud)
使用 GET 请求方法:
@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3}", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
public ResponseEntity<MyResponseType> getMyResource(
@ApiParam(value = "...",...)
@PathVariable("value1") String value1,
@ApiParam(value = "...",...)
@PathVariable("value2") String value2,
@ApiParam(value = "...",...)
@PathVariable("value3") String value3) {
//...
}
Run Code Online (Sandbox Code Playgroud)
我希望这个方法可以通过以下方式调用:
http://myserver:8080/myresource/value1/value2/value3
Run Code Online (Sandbox Code Playgroud)
但它只能通过斜杠到达:
http://myserver:8080/myresource/value1/value2/value3/
Run Code Online (Sandbox Code Playgroud)
为什么会这样或者是什么导致了这种情况?
Swagger 假设没有尾部斜杠,我现在无法使用 swagger 发送请求。
我该怎么做才能使第一个 URL 工作而不使第二个 URL 工作?
非常感谢您的评论和回答。
编辑 2:
我发现没有斜线 value3 的信息是不完整的。value3 必须是电子邮件地址,但从最后一个点开始的所有内容都被切断了。因此,我收到的不是“myemail@something.de”,而是“myemail@something”。
这解释了为什么我无法获得正确的结果(无内容和 HTTP 状态代码 404)。但我仍然不明白为什么会发生这种情况。此外,如果我的电子邮件地址在顶级域(如 .com)中包含三个字母,则请求永远不会到达 GET 方法“getMyResource”...... …
在使用Spring和REST API时遇到一个有趣的问题,那个问题是:在Spring中,路径是否限制为一定数量的字符?
代码如下
@RequestMapping(value = {REST_PREFIX + "/{key}"}, method = {RequestMethod.GET})
public DashboardItem getExceptionByKey(@PathVariable("key") String key, HttpServletRequest request, HttpServletResponse httpResponse_p) {
log.info("URL is {}", request.getRequestURL());
log.info("Key is {}", key);
return InspectionUtils.getExceptionByKey(key);
}
Run Code Online (Sandbox Code Playgroud)
密钥的示例是
67E4D2C089CBCCA2A9732F6986124C6B10.243.2.107#0EEE5EAB06ED4FFF82A8934F7058860C#79A2F0C170A028A3B0410F0F16303F41
Run Code Online (Sandbox Code Playgroud)
发送请求时,请确保对URL进行编码,并在程序中接收以下URL
/rest/exceptions/67E4D2C089CBCCA2A9732F6986124C6B10.243.2.107#0EEE5EAB06ED4FFF82A8934F7058860C#79A2F0C170A028A3B0410F0F16303F41
Run Code Online (Sandbox Code Playgroud)
因此,我收到了Hole键,但是当它解析它时,可变键只是
67E4D2C089CBCCA2A9732F6986124C6B10.243.2
Run Code Online (Sandbox Code Playgroud)
我以为可能是特殊字符,但看起来不像。我的第二个猜测是路径的长度受到限制。
因此,我对您的问题是有关路径的限制还是其他问题?
谢谢
什么是:.+在{param:.+}这个组中的java代码是什么意思?我试过搜索但是我找不到任何解释.知道的人请向我解释一下.非常感谢.
BatchFileController.java
@RequestMapping("/runbatchfileparam/{param:.+}")
public ResultFormat runbatchFile(@PathVariable("param") String fileName)
{
RunBatchFile rbf = new RunBatchFile();
return rbf.runBatch(fileName);
}
Run Code Online (Sandbox Code Playgroud) spring ×6
java ×5
spring-mvc ×5
rest ×4
annotations ×1
get ×1
hibernate ×1
json ×1
path ×1
spring-4 ×1
spring-json ×1
url ×1