Spring请求映射到特定路径变量值的不同方法

时间:2015-09-28 11:27:51

标签: java spring spring-mvc controller

@Controller
@RequestMapping("/authors")
public class AuthorController {
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Author getAuthor(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author by id
        return null;
    }

    @RequestMapping(value = "/{id}/author-properties", method = RequestMethod.GET)
    public AuthorProperties getAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author's List of properties
        return null;
    }

    @RequestMapping // How to map /authors/*/author-properties to this method ????
    public List<AuthorProperties> listAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response)
    {
        // Returns a single Author's List of properties
        return null;
    }
}

class Author {
    String propertiesUri;
    // other fields
}

class AuthorProperties {
    String authorUri;
    // other fields
}

基本上我需要:

  • / authors - 列出所有作者
  • / authors / 123 - 通过id 123
  • 获取作者
  • / authors / 123 / author-properties - 获取123作者的AuthorProperties对象
  • / authors / * / author-properties - 获取所有作者的AuthorProperties列表

当我尝试

@RequestMapping(value = "/*/author-properties", method = RequestMethod.GET)

它仍然将/authors/*/author-properties映射到getAuthorProperties方法,路径变量值为&#34; *&#34;。

3 个答案:

答案 0 :(得分:0)

看看是否有效

Adobe Air

答案 1 :(得分:0)

您可以使用正则表达式约束单个作者的映射,例如:

 java.lang.reflect.UndeclaredThrowableException
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at $Proxy1.userDelete(Native Method)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.example.nmsapp.CustomUserAdapter$DeleteUsers.doInBackground(CustomUserAdapter.java:130)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.example.nmsapp.CustomUserAdapter$DeleteUsers.doInBackground(CustomUserAdapter.java:107)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ Caused by: java.net.ProtocolException: DELETE does not support writing
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:258)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:86)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:128)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
09-28 15:32:03.133  12335-12824/com.example.nmsapp W/System.err﹕ at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:91)

这只会匹配作者ID为数字的网址。

对于所有作者属性的请求,您可以使用:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Hovewer @RequestMapping("/{authorId:\\d+}/author-properties") public String authorProperties(@PathVariable String authorId) {} 具有特殊含义,因此它也会匹配@RequestMapping("/*/author-properties") public String allProperties() { } 。要解决它,你可以使用类似的东西:

*

答案 2 :(得分:0)

如果为所有作者提取AuthorProperties列表是常见的情况,那么我认为您应该创建一个这样的URI:&#34; / author-properties&#34;。否则Bohuslav给出的答案就是你想要的。