草庐IT

ResponseBody

全部标签

java - 如何在返回 String 的 Spring MVC @ResponseBody 方法中响应 HTTP 400 错误

我将SpringMVC用于一个简单的JSONAPI,使用基于@ResponseBody的方法,如下所示。(我已经有一个直接生成JSON的服务层。)@RequestMapping(value="/matches/{matchId}",produces="application/json")@ResponseBodypublicStringmatch(@PathVariableStringmatchId){Stringjson=matchService.getMatchJson(matchId);if(json==null){//TODO:howtorespondwithe.g.400"b

java - 如何在返回对象的 Spring MVC @RestController @ResponseBody 类中响应 HTTP 状态代码?

我正在尝试让@RestController接受@PathVariable以JSON格式返回特定对象以及正确的状态代码。到目前为止,代码的方式是返回JSON格式的对象,因为它默认使用Spring4内置的Jackson库。但是我不知道如何制作它,所以它会向用户发送一条消息,说我们想要一个api变量,然后是JSON数据,然后是错误代码(或成功代码,取决于一切是否顺利)。示例输出为:Pleaseenterapivalueasparameter(NOTEthiscanbeinJSONaswellifneeded){"id":2,"api":"3000105000"...}(NOTEthiswil

java - 如何在返回对象的 Spring MVC @RestController @ResponseBody 类中响应 HTTP 状态代码?

我正在尝试让@RestController接受@PathVariable以JSON格式返回特定对象以及正确的状态代码。到目前为止,代码的方式是返回JSON格式的对象,因为它默认使用Spring4内置的Jackson库。但是我不知道如何制作它,所以它会向用户发送一条消息,说我们想要一个api变量,然后是JSON数据,然后是错误代码(或成功代码,取决于一切是否顺利)。示例输出为:Pleaseenterapivalueasparameter(NOTEthiscanbeinJSONaswellifneeded){"id":2,"api":"3000105000"...}(NOTEthiswil

java - Spring :返回@ResponseBody "ResponseEntity<List<JSONObject>>"

在Controller中,我创建了json数组。如果我返回List没关系:@RequestMapping(value="",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)public@ResponseBodyListgetAll(){ListentityList=entityManager.findAll();Listentities=newArrayList();for(Entityn:entityList){JSONObjectentity=newJSONObject();entity.put(

java - Spring :返回@ResponseBody "ResponseEntity<List<JSONObject>>"

在Controller中,我创建了json数组。如果我返回List没关系:@RequestMapping(value="",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)public@ResponseBodyListgetAll(){ListentityList=entityManager.findAll();Listentities=newArrayList();for(Entityn:entityList){JSONObjectentity=newJSONObject();entity.put(

json - 使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

我不喜欢jackson。我想使用ajax,但要使用GoogleGson。所以我试图弄清楚如何实现我自己的HttpMessageConverter以将其与@ResponseBody注释一起使用。有人可以花点时间告诉我我应该走的路吗?我应该打开哪些配置?另外我想知道我是否可以这样做并且仍然使用?提前致谢。大约3天前,我已经在SpringCommunityForuns中询问过,但没有得到答复,所以我在这里询问是否有更好的机会。SpringCommunityForumslinktomyquestion我还在网上进行了详尽的搜索,发现了一些关于这个主题的有趣内容,但似乎他们正在考虑将其放在Spr

json - 使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

我不喜欢jackson。我想使用ajax,但要使用GoogleGson。所以我试图弄清楚如何实现我自己的HttpMessageConverter以将其与@ResponseBody注释一起使用。有人可以花点时间告诉我我应该走的路吗?我应该打开哪些配置?另外我想知道我是否可以这样做并且仍然使用?提前致谢。大约3天前,我已经在SpringCommunityForuns中询问过,但没有得到答复,所以我在这里询问是否有更好的机会。SpringCommunityForumslinktomyquestion我还在网上进行了详尽的搜索,发现了一些关于这个主题的有趣内容,但似乎他们正在考虑将其放在Spr

java - 在 Spring MVC 中,如何在使用 @ResponseBody 时设置 mime 类型 header

我有一个返回JSON字符串的SpringMVCController,我想将mimetype设置为application/json。我该怎么做?@RequestMapping(method=RequestMethod.GET,value="foo/bar")@ResponseBodypublicStringfooBar(){returnmyService.getJson();}业务对象已作为JSON字符串提供,因此使用MappingJacksonJsonView不是我的解决方案。@ResponseBody已经很完美了,但是如何设置mimetype呢? 最佳答案

java - 在 Spring MVC 中,如何在使用 @ResponseBody 时设置 mime 类型 header

我有一个返回JSON字符串的SpringMVCController,我想将mimetype设置为application/json。我该怎么做?@RequestMapping(method=RequestMethod.GET,value="foo/bar")@ResponseBodypublicStringfooBar(){returnmyService.getJson();}业务对象已作为JSON字符串提供,因此使用MappingJacksonJsonView不是我的解决方案。@ResponseBody已经很完美了,但是如何设置mimetype呢? 最佳答案

java - Spring @ResponseBody 注释是如何工作的?

我有一个方法用以下方式注释:/***Providealistofallaccounts.*///TODO02:Completethismethod.Addannotationstorespond//toGET/accountsandreturnaListtobeconverted.//Saveyourworkandrestarttheserver.YoushouldgetJSONresultswhenaccessing//http://localhost:8080/rest-ws/app/accounts@RequestMapping(value="/orders",method=Re