2016-03-14 34 views
0

我對Spring Webmvc有一些疑問。我試圖用一個平靜的web服務製作一個web應用程序,但我不知道它是否正確工作。如何測試Spring Webmvc web服務休息

我有這個類的maven-webapp項目:

... 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 
... 

@RestController 
@RequestMapping("/service") 
public class PersoWsRest { 

    @RequestMapping(value = "/lister", method = RequestMethod.GET) 
    public List<Perso> listerPersos() { 
     System.out.println("PASSAGE DS LE WEB SERVICE\n"); 

     List<Perso> res = new ArrayList<Perso>(); 

     res.add(new Perso("Test", "Un")); 
     res.add(new Perso("Test", "Deux")); 
     res.add(new Perso("Test", "Trois")); 

     return res; 
    } 

} 

,當我啓動我的Tomcat服務器(沒有錯誤),我不能管理試試我的Web服務。

我認爲,這是兩個問題:

  • 我不使用正確的URL,但我想很多URL的...所以...

  • 我不t使Spring WebMvc的配置正確。

你有什麼想法嗎?謝謝。

+2

請指定3件事情:您的Web應用程序上下文路徑(Web應用程序路徑),調度程序Servlet的URL映射以及您用於訪問此服務的URL。 –

+0

在任何人都可以幫到你之前,你需要添加更多的細節 –

+0

你的本地tomcat運行8080的端口是什麼?上下文路徑是什麼?該URL應該像localhost:8080/webappcontext/server/lister,但沒有更多的細節很難提供幫助。 – slarge

回答

0

對於那些正在尋找示例代碼的人來說,這裏是。

@RunWith(SpringJUnit4ClassRunner.class) 
@WebMvcTest(PersoWsRest.class) 
@AutoConfigureMockMvc 
public class PersoWsRestTest{ 

@Autowired 
MockMvc mockMvc; 

@Test 
public void testListerPersos(){ 
    MvcResult mvcResult = this.mockMvc.perform(get("/service/lister") 
      .andExpect(status().isOk()) 
      .andReturn(); 
//..... Further validation 
     }  
    }