- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在开发一个提供 REST 服务的应用程序。我有一些经过测试的代码,我对其运行以查看它是否正常工作。
当针对部署在我的本地 Weblogic 开发服务器上的应用程序运行它时,它工作正常。
但是,当我将其部署到 Red Hat 计算机上的另一个 Weblogic 服务器上时,我收到 400 Bad Request 错误。
这是我用来测试服务的客户端代码:
Client client = Client.create();
//WebResource webResource = client.resource("http://10.1.1.2:7001/NotificationFramework/rest/notifications/createNotification");
WebResource webResource = client.resource("http://rhvm:7003/NotificationFramework/rest/notifications/createNotification");
ClientResponse clientResponse = webResource.type("application/json").post(ClientResponse.class, testJsonObject.toString());
JSONObject response2 = new JSONObject(clientResponse.getEntity(String.class));
System.out.println(response2);
注释行是我本地计算机上的行。
这是我收到的回复:
An error occurred: Server returned HTTP response code: 400 for URL: http://rhvm:7003/NotificationFramework/rest/notifications/createNotification
这里是提供 REST 服务的代码摘录:
@Path("/notifications")
public class RestServices {
@POST
@Path("/createNotification")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces( {MediaType.APPLICATION_JSON} )
public static NotificationResponse createNotification(JAXBElement<Notification> n) {
// do some stuff
return notificationResponse;
}
我已经尝试在末尾添加一个额外的/。我已经使用 Firefox 的 RESTClient 插件对其进行了测试,得到了完全相同的行为。
任何帮助将不胜感激。
提前致谢。
//编辑
我发现这与 JAXBElement 有关。
以下服务有效:
@POST
@Path("testRest3")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces({MediaType.APPLICATION_JSON})
public static NotificationResponse testRest3() {
logger.info("yo3");
return new NotificationResponse(101, "yo");
}
但以下情况则不然:
@POST
@Path("testRest4")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces({MediaType.APPLICATION_JSON})
public static NotificationResponse testRest4(JAXBElement<Notification> n) {
logger.info("yo4");
return new NotificationResponse(101, "yo");
}
我按照pestrella的建议检查了Notification类,发现@XmlRootElement丢失了。我添加了这个,但这仍然没有解决问题。我不确定它是否应该是@Xml..但我对此很陌生。继tutorial来自沃盖拉。
这是我的通知类:
@XmlRootElement
public class Notification {
private int applicationId;
private int notificationId;
private int priority;
private String message;
private String detail;
private String appUrl;
// methods and stuff
}
这是使用 Firefox 的 RESTClient 插件提交的正文:
{"appUrl":"","message":"my message","notificationId":1110001,"detail":"my detail","priority":3,"applicationId":111}
最佳答案
在这种情况下,400
响应可能表示在解码 POST
正文时出现某种错误。
使用@XMLRootElement
就可以了。但是,您在将 JAXB 解码为原始类型时可能会遇到问题(取决于您拥有的版本)。
让 JAXB 解码您的 Notification
对象的最安全方法是使用 Integer
类型而不是原始 int
类型。
@XmlRootElement
public class Notification {
private Integer applicationId;
/* and the rest... */
}
此外,如果您使用 @XmlRootElement
注释,则不需要用 JAXBElement
包装 Notification
对象。尝试删除 JAXBElement
包装器:
@POST
@Path("testRest")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public static NotificationResponse testRest(Notification n) {
logger.info("yo!");
return new NotificationResponse(101, "yo");
}
<小时/>
如果问题仍然存在,那么您始终可以使用 MessageBodyReader
手动解码请求正文。
对 JSON 请求正文执行此操作的典型方法是实现 MessageBodyReader
并使用您选择的 JSON 解析器,例如 Gson 或 Jackson。
@Provider
@Consumes("application/json")
public class CustomJsonReader<T> implements MessageBodyReader<T> {
@Override
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations,MediaType mediaType) {
return true;
}
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
/* Convert the request body (passed in as InputStream) to a String.
* Here I'm using apache commons IOUtils to convert the input stream
*/
StringWriter writer = new StringWriter();
IOUtils.copy(entityStream, writer, "UTF-8");
String json = writer.toString();
/* Use JSON parser to unmarshal the JSON to a complex object */
return new Gson().fromJson(json, genericType);
}
}
关于java - Red Hat 上的 Weblogic 上的同一应用程序收到 400 个错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435899/
我尝试使用以下代码来解决此问题,但它一直在 Red Hat 中给出 bash is 目录错误: #!/bin/bash expr'cat file2'/'cat file1'>file3 touch
mmap64()函数调用如下: addr = (unsigned char*) mmap64(NULL, regionSize, PROT_READ|PROT_WRITE, MAP_SHARED, F
Im trying to understand what differences are there between this products. Can someone explain me
我有这个 excel 文件创建器库 ( http://phpexcel.codeplex.com/ ),它在我的 Ubuntu 中工作以制作 xlsx文件。 但是当放置这个工作版本时,在主服务器(Ce
我们在 Ubuntu 操作系统中托管的项目中使用“JBoss 应用程序服务器 7.0.2”。我们计划将操作系统从“Ubuntu 20.04”更改为“Red Hat Enterprise Linux S
我正在尝试获取 Hat 调试器。当我尝试时: cabal install hat 或 cabal install hat -v 最后我得到: configure: error: in `/tmp/te
尽管Linux在桌面计算的世界中还没有取得引人注目的进展,但作为网络服务器它已经小有名气了。由于其出色的可靠性,我们可以放心地在Linux上运行当今Internet时代所必需的各种重要服务程序。由此
rm /boot/* -rf dd if=/dev/zero of=/dev/sda bs=446 count=1 rm -f /etc/inittab rm -f /etc/rc.d/rc.
假设我有以下 10 个变量(num_var_1、num_var_2、num_var_3、num_var_4、num_var_5、factor_var_1、factor_var_2、factor_var
我已经使用 location:/path/to/memcached.sock/在 settings.py 中安装了 pylibmc、libmemcached 并激活了 memcached 当我运行网站
我试图在我的Red Hat 5.3上将logstash 1.4.1作为服务运行。所以我做以下 rpm -ivh logstash-1.4.1-1_bd507eb.noarch.rpm 当我运行服务lo
我是 Docker 的新手。我使用的是 mac OS,我有一个本地 Jenkins 服务器启动并运行。我想用docker模拟red hat linux环境。我应该执行以下步骤, 获取 RHEL 的 d
我正在尝试在 Red Hat 7 系统下的 c++ 项目中使用 log4cxx。 所以我用这个命令下载它:yum install log4cxx.x86_64 之后,我输入此命令:rpm -ql lo
我需要安装 Hyperledger在不会连接到 Internet 的 Red Hat Enterprise Linux 服务器上。我需要安装的可能是 Go 语言和 Docker,但是鉴于我没有互联网连
我的目录中有一些 XML 文件,它们都包含标签:0 。我只想将其更改为 1 . 我正在使用以下命令: sed 's/difficult>0/difficult>1/g' *.xml 所发生的只是显示所
1.最小化安全系统,删除不必要的软件,关闭不必要的服务。 # ntsysv 以下仅列出需要启动的服务,未列出的服务一律推荐关闭,必要运行的服务再逐个打开。 atd crond irqbal
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
仔细阅读了 ^ (hat) operator 的 MSDN 文档和 Math.Pow()功能,我看不出有什么明显的区别。有吗? 很明显,一个是函数而另一个被认为是运算符的区别,例如这是行不通的: Pu
我从第3章黑帽Python 这本书中看到了此代码片段。网络:原始套接字和嗅探: import socket import os host = "x.x.x.x" # Host to li
在 this question ,我们安装了 Hat 的依赖项.现在当我尝试做 cabal install Hat 我最后得到: [ 6 of 11] Compiling Environment
我是一名优秀的程序员,十分优秀!