从 Red Hat 上的 Weblogic 上的同一应用程序获取 400 个错误请求 [英] Getting 400 bad request from same application on Weblogic on Red Hat

查看:68
本文介绍了从 Red Hat 上的 Weblogic 上的同一应用程序获取 400 个错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个提供 REST 服务的应用程序.我有一些经过测试的代码,可以针对它运行以查看它是否可以正常工作.

I've been developing an application that provides a REST service. I have some tested code that I run against it to see if it works okay.

针对部署在我本地 Weblogic 开发服务器上的应用程序运行它时,它工作正常.

When running it against the application deployed on my local Weblogic development server, it works fine.

但是,当我将它部署到 Red Hat 机器上的另一个 Weblogic 服务器上时,我收到 400 Bad Request 错误.

However, when I deployed it on another Weblogic server on a Red Hat machine, I get 400 Bad Request errors.

这是我用来测试服务的客户端代码:

Here is the client code I'm using to test the service:

    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);

注释行是我本地机器上的行.

The commented line is the one on my local machine.

这是我得到的回复:

An error occurred: Server returned HTTP response code: 400 for URL: http://rhvm:7003/NotificationFramework/rest/notifications/createNotification

这里是提供 REST 服务的代码摘录:

And here is an excerpt of the code providing the REST service:

@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 插件对其进行了测试,我得到了完全相同的行为.

I've already tried putting an extra / on the end. And I've tested it with the RESTClient add-on for Firefox and I get the exact same behaviour.

任何帮助将不胜感激.

提前致谢.

//编辑

我发现这与 JAXBElement 有关.

I discovered that it's something to do with the 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");
}

我按照pesrella 的建议检查了Notification 类,发现缺少@XmlRootElement.我添加了这个,但这仍然没有解决问题.我不确定它是否应该是@Xml .. 但我是新手.遵循 vogella 的教程.

I checked the Notification class as recommended by pestrella and found that @XmlRootElement was missing. I added this but this still hasn't fixed the problem. I'm not sure if it should be @Xml.. but I'm new to this. Following the tutorial from vogella.

这是我的通知类:

@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 附加组件提交的正文:

And here is the body as submitted with the RESTClient add-on for Firefox:

{"appUrl":"","message":"my message","notificationId":1110001,"detail":"my detail","priority":3,"applicationId":111}

推荐答案

此实例中的 400 响应可能表示在解组 POST 正文时出现某种错误.

A 400 response in this instance can indicate some sort of error while unmarhsalling the POST body.

使用 @XMLRootElement 没问题.但是,让 JAXB 解组为原始类型可能会遇到问题(取决于您使用的版本).

Using the @XMLRootElement is fine. However, you might have problems getting JAXB to unmarshal to primitive types (depends on what version you've got).

让 JAXB 解组 Notification 对象的最安全方法是使用 Integer 类型而不是原始 int 类型.>

The safest way to get JAXB to unmarshal your Notification object is to use the Integer type instead of a primitive int type.

@XmlRootElement
public class Notification {
    private Integer applicationId;

    /* and the rest... */
}

此外,如果您使用 @XmlRootElement 注释,则不需要用 JAXBElement 包装 Notification 对象.尝试删除 JAXBElement 包装器:

Also, you should not need to wrap the Notification object with JAXBElement if you are using the @XmlRootElement annotation. Try removing the JAXBElement wrapper:

@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.

The typical way to do this for JSON request bodies is to implement MessageBodyReader and use a JSON parser of your choice, like Gson or 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);
    }
}

这篇关于从 Red Hat 上的 Weblogic 上的同一应用程序获取 400 个错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆