没有URI.json格式的映射GET
我不知道为什么URI.json的GET Message没有映射 但是没有“.json”的情况下运行良好 我试着在Junit4中使用MockMvc测试没有URI.json格式的映射GET,json,spring,restapi,Json,Spring,Restapi,我不知道为什么URI.json的GET Message没有映射 但是没有“.json”的情况下运行良好 我试着在Junit4中使用MockMvc测试 @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest class WriteActionControllerTest { @Autowired WriteActionController writeActionController; MockMvc mockM
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class WriteActionControllerTest {
@Autowired
WriteActionController writeActionController;
MockMvc mockMvc;
@BeforeEach
void setUp()
{
mockMvc = MockMvcBuilders.standaloneSetup(writeActionController).build();
}
@Test
void getCommentsList() throws Exception {
RequestBuilder requestBuilder =
MockMvcRequestBuilders.get("/view/2/comments.json").contentType(MediaType.APPLICATION_JSON);
mockMvc.perform(requestBuilder).andExpect(status().isOk()).andDo(print());
}
此代码包含SpringController.class
@RestController
public class WriteActionController {
@GetMapping("/view/{postNumber}/comments")
public List<CommentsDTO> getCommentsList(@PathVariable int postNumber){
return viewService.select_CommentsByPostNumber(postNumber);
}
}
@RestController
公共类WriteActionController{
@GetMapping(“/view/{postNumber}/comments”)
公共列表getCommentsList(@PathVariable int postNumber){
返回viewService。选择_CommentsByPostNumber(postNumber);
}
}
但是,在Junit4中使用MockMvcTest时,我得到了这个成功的响应消息。
我想回答为什么GET请求不能在浏览器上工作
MockHttpServletRequest:
HTTP Method = GET
Request URI = /view/2/comments.json
Parameters = {}
Headers = [Content-Type:"application/json"]
Body = <no character encoding set>
Session Attrs = {}
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = [{"comments_id":1,"comments_content":"hello world","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":2,"comments_content":"test","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":3,"comments_content":"uytyu","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":4,"comments_content":"tewqwes","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":5,"comments_content":"testtest","_usr_email":"mokaim@naver.com","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":6,"comments_content":"testtest","_usr_email":"mokaim@naver.com","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":7,"comments_content":"test","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":8,"comments_content":"wewer","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"}]
Forwarded URL = null
Redirected URL = null
Cookies = []
MockHttpServletRequest:
HTTP方法=获取
请求URI=/view/2/comments.json
参数={}
Headers=[内容类型:“应用程序/json”]
正文=
会话属性={}
MockHttpServletResponse:
状态=200
错误消息=null
Headers=[内容类型:“应用程序/json”]
内容类型=应用程序/json
正文=[{“评论id”:1,“评论内容”:“你好,世界”,“发布数量”:2,“注册日期”:“2020-07-26”},{“评论id”:2,“评论内容”:“测试”,“发布数量”:2,“发布日期”:“2020-07-26”},{“评论id”:2,“评论内容”:“测试”,“发布数量”:“管理”,“发布数量”:2,“注册日期”:“2020-07-26”},{“评论id”:3,“评论内容”:“uytyu,“评论内容”:“tewqwes”,“usr邮件”:“admin”,“post数量”:2,“注册日期”:“2020-07-26”},{“评论id”:5,“评论内容”:“testtest”,“usr邮件”:mokaim@naver.com“,”发布编号“:2,”注册日期“:”2020-07-26“},{”评论id“:6,“评论内容“:”测试“,”usr\U电子邮件“:”mokaim@naver.com“,”发布编号“:2,“注册日期“:”2020-07-26“},{”评论id“:7,“评论内容“:”测试“,”电子邮件“:”管理“,”发布数量“:2,“注册日期“:”2020-07-26“},{”评论id“:8,“评论内容“:”WEWEWER“,”电子邮件“:”管理“,”发布数量“:2,“注册日期“:”2020-07-26“}]
转发的URL=null
重定向的URL=null
Cookies=[]
嗯,我不太确定我是否收到了你的问题。问题是关于RequestMapping的吗?因此你在这里有一个控制器:
@GetMapping("/view/{postNumber}/comments")
因此,您的应用程序只在comments
中侦听,而不在comments.json
上侦听。如果需要,您可以向映射添加多个值
@RequestMapping(value={"/view/{postNumber}/comments", "/view/{postNumber}/comments.json"})
这里也给出了关于Spring的精彩解释。嗯,我不太确定我是否收到了你的问题。这个问题是关于RequestMapping的吗?所以你在这里有一个控制器:
@GetMapping("/view/{postNumber}/comments")
因此,您的应用程序只在comments
中侦听,而不在comments.json
上侦听。如果需要,您可以向映射添加多个值
@RequestMapping(value={"/view/{postNumber}/comments", "/view/{postNumber}/comments.json"})
这里也给出了关于Spring的精彩解释。因为您的测试和实际代码不同。您应该自动连接
MockMvc
以获得与重用实际web配置相同的结果,您的配置将创建一个新的配置。因为您的测试和实际代码不同。您应该自动连接MockMvc
为了获得与重用实际web配置相同的结果,您的配置将创建一个新的配置。