2016-10-31 7 views
0

Spring MVC Webアプリケーションがあります。今私はSpring RESTを使ってWebサービスとして私のサービスを公開したいと思っています。これを行うには、URL値に基づいてWebリクエストとRESTリクエストを処理する必要があります。以下では、以下に示す3つのコントローラ、MasterController、PatientController、PatientRESTControllerを使って同じことを試みました。簡潔さのためにメソッドをスキップしました。Spring MVCでのWebリクエストとRESTリクエストのリクエストマッピングの分離

@Controller("/") 
public class MasterController { 

@RequestMapping("/web") 
public ModelAndView webApplication(){ 
    return new ModelAndView("redirect:/web/patient"); 
} 

@RequestMapping("/rest") 
public ModelAndView webService(){ 
    return new ModelAndView("redirect:/rest/patient"); 
} 
} 

@Controller("/web/patient") 
public class PatientController { 

@GetMapping("") 
public ModelAndView patientHome(){ 
    ModelAndView mv = new ModelAndView(); 
    mv.setViewName("patienthome"); 
    return mv; 
} 
} 

@RestController("/rest/patient") 
public class PatientRESTController { 

@GetMapping("") 
public List getAllPatientsREST(){ 
    return patientService.findAll(); 
} 
} 

私のWebアプリケーションを起動するには、私はエラーを取得しています:

Ambiguous mapping. Cannot map '/rest/patient' method public java.util.List PatientRESTController.getAllPatientsREST() to {[],methods=[GET]}: There is already '/web/patient' bean method

私はRESTやWebアプリケーションのための別のURLマッピングを作成するにはどうすればよいです?

答えて

0

私は問題がスローされた例外による@GetMapping("") に空の文字列から来て、両方のケースでは、スプリングによって解決マッピングは、あなたの残りのコントローラの空であることから、それは相対的なマッピングではないと思われる:

Ambiguous mapping. Cannot map '/rest/patient' method public java.util.List PatientRESTController.getAllPatientsREST() to {[],methods=[GET]}: There is already '/web/patient' bean method

getMappingアノテーションに値を指定する必要があります。

@RestController 
@RequestMapping("/rest/patients") 
public class PatientRESTController { 

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<?> getAll(HttpServletRequest request, HttpServletResponse response) { 
    ... 
    } 
    } 
:個人的に

@RestController 
public class PatientRESTController { 

@GetMapping("/rest/patients") 
public List getAllPatientsREST(){ 
    return patientService.findAll(); 
} 
} 

が、私はそのような私のRestControllerを宣言します:あなたはそれまたは別のものを試すことができます

関連する問題