Jersey/JAXB XML 验证错误的自定义错误消息

发布于 2024-12-22 16:20:37 字数 1012 浏览 2 评论 0原文

所以我在 Jersey 上设置了 REST 服务。

我的 POJO 只是注释为 @XmlRootElement

我向 REST 服务发送了一个 POST 请求,一切正常。

1-我将格式错误的 XML 发送到服务,我得到容器默认的 400 错误请求页面。使用 Glassfish 3 我记不清了,但我知道我可以更改或告诉我的网络应用程序映射到我自己的 400 错误请求,对吧?我在哪里可以找到有关该特定主题的文档。

2-我发送了有效的 XML,但在预期字段之一中我输入了空值。我的服务尝试执行某些操作,但抛出了持久性异常。我返回容器默认的 500 内部服务器错误页面,它列出了抛出的异常。我想我也可以映射到我自己的自定义错误页面,但我不仅仅是想显示异常消息。这被认为是一个安全问题。

我基本上想根据我的业务逻辑自己验证字段,并向客户端返回一条错误消息,例如“Field1 无效”仅此而已。并且客户端应该能够解析错误代码并知道哪个字段是错误的。

另外,当我的服务出现不止一种类型的错误请求错误时,我该怎么办?例如,该字段可以期望 1-3 之间的 INT 值,但特定业务请求期望值为 2,并且发送 3。

因此,对于类型/值验证,我只想简单地说“此处的字段名称无效”。但对于特定的业务逻辑,例如“帐户无效”或“帐户已过期”或“地址不匹配”等...

或者当我有超过 1 个成功代码时怎么样?

基本上,我采用旧的“xml”类型服务并转换为 REST 样式以进行概念验证并使其达到标准。旧的几乎是 100% POST,即使“查询结果。即:GET。所以基本上你向它 POST XML,它读取 XML,调用业务逻辑,逻辑在 XML 响应中返回适当的代码。所以一切都是HTTP 200 OK 并且业务逻辑代码在 XML 响应中返回,因此客户端收到 500 错误的唯一情况是出现服务无法生成 XML 响应的硬故障,甚至无效的 XML 也会返回为 200。好的,但是 XML 响应用我自己的错误代码表明了这一点,

不久前有人建议我使用 SOAP 来实现更多面向“消息”的服务,但我仍然认为它太重了,我可以使用 REST 来实现我的服务所做的事情,即使我不需要大部分休息?

So I have a REST service setup with Jersey.

My POJO is simply annotated as @XmlRootElement

I send a POST request to my REST service and everything works fine.

1- I send malformed XML to the service I get back the containers default 400 Bad Request page. Using Glassfish 3 I can't remember exactly but I know I can change or tell my web app to map to my own 400 bad request right? Where can I look for docs on that specific subject.

2- I send valid XML but in one of the expected fields I put empty value. My service tries to do something and it throws a persistence exception. I get back the containers default 500 Internal Server Error page and it list the exception thrown. I guess I can map to my own custom error page as well, but I do not just want to show the exception message. This is considered a security issue.

I would basically like to validate the fields myself based on my business logic and return back to the client an error message such as "Field1 is invalid" Nothing more nothing less. And the client should be able to parse the error code and also know which field was wrong.

Also what can I do when my service has more then one type of bad request error? For instance, the field can expect an INT value between 1-3, but specific business request expects value of 2 and a 3 is sent.

So for type/value validation I want to simply say "Invalid field name here". But for specific business logic say for instance "Invalid account" or "Account Expired" or "Address Does Not Match" etc...

Or how about when I have more then 1 success code?

Basically I'm taking an old "xml" type service and converting to REST style for proof of concept and bring it up to standards. The old one pretty much is 100% POST even when "querying for results. I.e: GET. So basically you POST to it XML, it reads the XML, calls the business logic and logic returns appropriate code in the XML response. So everything is HTTP 200 OK and the business logic code is returned in an XML response. So the only time a client gets a 500 error is when there is hard failure where the service can't produce an XML response. Even invalid XML is returned as 200 OK, but the XML response back indicates this with my own errors codes.

A while back I was suggested I use SOAP for more "message" oriented services, but I still think it's to heavy and I can achieve with REST what my service does, even though I don't need most of REST?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2024-12-29 16:20:37

您可以使用 ExceptionMappers 将异常映射到 HTTP 响应 - 请参阅:
http://jersey.java.net/nonav/documentation/1.11 /jax-rs.html#d4e433
http://jsr311.java.net/nonav /releases/1.1/javax/ws/rs/ext/ExceptionMapper.html

如果您想在请求到达资源方法之前进行验证,您可以尝试为此编写一个 ContainerRequestFilter 并抛出一个 WebApplicationException ,它将 Response 作为参数- jersey 会自动将该异常映射到其初始化的响应。

You can map exceptions to HTTP responses using ExceptionMappers - see:
http://jersey.java.net/nonav/documentation/1.11/jax-rs.html#d4e433
http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/ext/ExceptionMapper.html

If you want to do validation before the request even reaches the resource method, you can try writing a ContainerRequestFilter for that and throw a WebApplicationException which takes Response as a parameter - jersey automatically maps that exception to the response it was initialized with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文