Java 格式错误的 JSON:Spring 中出现意外的“<”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32929810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Malformed JSON: Unexpected '<' in Spring
提问by Logical Error
I want to produce json
from JPA @Entity
, I have
我想json
从 JPA生产@Entity
,我有
@Entity
@JsonAutoDetect
public class Bar implements Serializable {
@Id
@GeneratedValue
private Integer id;
private String title;
//omitting other stuff
}
my controller is
我的控制器是
@RestController
public class BarController {
@Autowired
private BarService barService;
@RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json", produces={"application/json"})
public List<Bar> list() {
return barService.findAllBars());
}
}
I'm having this error in browser
我在浏览器中遇到此错误
what is wrong with it.
它有什么问题。
采纳答案by Arshad Ali
I caught up one fatal mistake which is you are getting list of Bars barService.findAllBars(), you may need to convert that list to json adding the method as
我发现了一个致命的错误,即您正在获取 Bars barService.findAllBars()列表,您可能需要将该列表转换为 json,并将方法添加为
public static String toJSON(Object object)
{
if ( object == null ){
return "{}";
}
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
catch (Exception e) {
e.printStackTrace();
}
return "{}";
}
Now make change as
现在改变为
@RequestMapping(value = "/", method = RequestMethod.GET, produces={"application/json"})
public String list() {
return toJSON(barService.findAllBars());
}
hope this works, if any issues feel free to query in comments session.
希望这有效,如果有任何问题可以在评论会话中随意查询。
回答by hermitmaster
I suspect there is something amiss with your accept header in your get request. Try setting the header to
我怀疑您的 get 请求中的 accept 标头有问题。尝试将标题设置为
"Accept=*/*"
and see what you get back.
看看你能得到什么。
回答by Andreas
The "Malformed JSON" message is from the "Pretty" printing. Click "Raw" to see the actual response.
“Malformed JSON”消息来自“Pretty”打印。单击“原始”以查看实际响应。
The actual response is a 406 Not Acceptable
error (says so on your screen) with a payload of HTML (hence the unexpected <
from "Pretty") that says the request has been rejected by the server.
实际响应是一个406 Not Acceptable
错误(在您的屏幕上这样说),带有 HTML 有效负载(因此是<
“Pretty”中的意外),表示请求已被服务器拒绝。
Remove the headers = "Accept=application/json"
from the @RequestMapping
. The produces={"application/json"}
is already telling Spring to only call this method if application/json
is an acceptable response, which it likely is, but the header might say *.*
, or something more complex like text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
, both of which will allow application/json
.
取出headers = "Accept=application/json"
从@RequestMapping
。在produces={"application/json"}
已经告诉春天,如果只调用此方法application/json
是可接受的响应,这很可能是,但头可能会说*.*
,还是更复杂的东西一样text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
,两者将允许application/json
。
Of course, since this is likely an AJAX call that is expecting JSON, it shouldhave listed only application/json
in the accept
value. Check the code executing the AJAX call if that is not the case.
当然,因为这可能是一个需要 JSON 的 AJAX 调用,所以它应该只application/json
在accept
值中列出。如果不是这种情况,请检查执行 AJAX 调用的代码。
回答by Farah Nawaz
Use JsonFormatter https://jsonformatter.curiousconcept.com/to test the JSON before proceeding further. Its a very good tool that validates JSON and shows you possible errors with line number.
在继续之前,使用 JsonFormatter https://jsonformatter.curiousconcept.com/测试 JSON。它是一个非常好的工具,可以验证 JSON 并显示行号可能出现的错误。