我正在編寫Spring服務器,並使用Retrofit進行api調用。Spring - 未找到具有URI的HTTP請求的映射
我對改造客戶端的下一個界面:
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
public interface AuthSvcApi {
public static final String AUTHENTICATION_PATH = "/authToken";
@POST(AUTHENTICATION_PATH)
public boolean loginUser(@Body String accessToken);
}
然後我的控制器:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.purposes.client.AuthSvcApi;
@Controller
public class AuthSvc{
@RequestMapping(value=AuthSvcApi.AUTHENTICATION_PATH, method = RequestMethod.POST)
public @ResponseBody boolean loginUser(@RequestBody String accessToken) {
CheckAccessToken checkAccessToken = new CheckFacebookAccessToken();
checkAccessToken.checkToken(accessToken);
return false;
}
}
的方法還沒有完成,但它應該工作。而應用程序類是:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//Tell Spring to automatically inject any dependencies that are marked in
//our classes with @Autowired
@EnableAutoConfiguration
//Tell Spring to turn on WebMVC (e.g., it should enable the DispatcherServlet
//so that requests can be routed to our Controllers)
@EnableWebMvc
//Tell Spring to go and scan our controller package (and all sub packages) to
//find any Controllers or other components that are part of our applciation.
//Any class in this package that is annotated with @Controller is going to be
//automatically discovered and connected to the DispatcherServlet.
@ComponentScan
//Tell Spring that this object represents a Configuration for the
//application
@Configuration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我不知道爲什麼,這並不正常工作,但我要瘋了,因爲反應是:
No mapping found for HTTP request with URI [/authToken] in DispatcherServlet with name 'dispatcherServlet'
我認爲這是沒有錯誤的,因爲終點是服務器的名稱。我會證明,無論如何;) – abemart 2014-09-07 17:33:19
這不是問題,問題是我需要Application類中的@EnableWebMvc註釋。現在,我還有其他問題,我收到消息'DispatcherServlet'中名爲'dispatcherServlet'的未使用URI [/ authToken]爲HTTP請求找到映射,因爲dispatcherServlet沒有映射我的URL。 – abemart 2014-09-08 09:31:09