简体   繁体   English

Spring 4.3.7和Weblogic 12.2.1 Rest集成有错误

[英]spring 4.3.7 and weblogic 12.2.1 Rest integration has error

i have an restful spring web app that i want to deploy in weblogic.deploying this in tomcat works fine but in weblogic i got this error: 我有一个宁静的Spring Web应用程序,我想在weblogic中进行部署。在tomcat中部署此方法效果很好,但是在weblogic中却出现此错误

Root cause of ServletException.
java.lang.NoSuchMethodError:   com.fasterxml.jackson.databind.ObjectWriter.forType(Lcom/fasterxml/jackson /databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectWriter;
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:278)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
Truncated. see log file for complete stacktrace

i have tried different jackson lib versions:2.6 2.7 2.7.6 2.8 2.8.7 2.8.8 and 2.9 but it has same error. 我尝试了不同的杰克逊库版本:2.6 2.7 2.7.6 2.8 2.8.7 2.8.8和2.9,但它具有相同的错误。 my controller class: package com.rest.springmvc.controller; 我的控制器类:包com.rest.springmvc.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.rest.springmvc.model.User;
import com.rest.springmvc.service.UserService;

@RestController
public class RestController {

@Autowired
UserService userService;  //Service which will do all data retrieval/manipulation work


//-------------------Retrieve All Users--------------------------------------------------------

@RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
    List<User> users = userService.findAllUsers();
    if(users.isEmpty()){
        return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
    }
    return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
//-------------------Retrieve Single User--------------------------------------------------------

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") long id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}



//-------------------Create a User--------------------------------------------------------

@RequestMapping(value = "/user/", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(@RequestBody User user,  UriComponentsBuilder ucBuilder) {
    System.out.println("Creating User " + user.getName());

    if (userService.isUserExist(user)) {
        System.out.println("A User with name " + user.getName() + " already exist");
        return new ResponseEntity<Void>(HttpStatus.CONFLICT);
    }

    userService.saveUser(user);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
    return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}


//------------------- Update a User --------------------------------------------------------

@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
    System.out.println("Updating User " + id);

    User currentUser = userService.findById(id);

    if (currentUser==null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }

    currentUser.setName(user.getName());
    currentUser.setAge(user.getAge());
    currentUser.setSalary(user.getSalary());

    userService.updateUser(currentUser);
    return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}

//------------------- Delete a User --------------------------------------------------------

@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
    System.out.println("Fetching & Deleting User with id " + id);

    User user = userService.findById(id);
    if (user == null) {
        System.out.println("Unable to delete. User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }

    userService.deleteUserById(id);
    return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
}


//------------------- Delete All User --------------------------------------------------------

@RequestMapping(value = "/user/", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteAllUsers() {
    System.out.println("Deleting All Users");

    userService.deleteAllUsers();
    return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
}

} }

application successfully deploys but when i post http://localhost:7001/user/ i got that error in weblogic. 应用程序成功部署,但是当我发布http:// localhost:7001 / user /时,我在weblogic中遇到了该错误。

The NoSuchMethodError indicates that the class no longer has a definition of that method . NoSuchMethodError指示该类不再具有该方法的定义 This could be due to library version invoked by Weblogic being different from your application's intended version. 这可能是由于Weblogic调用的库版本与应用程序的预期版本不同。

Finding the jar : 找到罐子

  1. Temporarily add a -verbose:class argument to the server's JVM arguments. 临时向服务器的JVM参数添加-verbose:class参数。
  2. In server's out file, you will be able to see the *.jar that is being loaded. 在服务器的out文件中,您将能够看到正在加载的* .jar。

If the jar version is different from the version you expected, you could consider upgrading your application to newer version of the library. 如果jar版本与预期的版本不同,则可以考虑将应用程序升级到该库的较新版本。 If that's not possible, a small workaround by using this parameter . 如果不可能,请使用此参数进行较小的解决。

it seems to be a classloader issues. 这似乎是一个类加载器的问题。 Try to isolate your webapp classloader by configuring it in order you want to use your webapp classloader first and then the parent classloader 尝试通过配置webapp类加载器来隔离它,以便先要使用webapp类加载器,然后再使用父类加载器

Try to add the weblogic.xml file to your application. 尝试将weblogic.xml文件添加到您的应用程序。 In this file, add the tag prefer-application-packages whith package com.fasterxml.jackson 在此文件中,添加标记com.fasterxml.jackson prefer-application-packages com.fasterxml.jackson

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">

     <wls:weblogic-version>12.1.1</wls:weblogic-version>
     <wls:context-root>FilterWeb</wls:context-root>
     <wls:container-descriptor>
         <wls:prefer-application-packages>
             <wls:package-name>com.fasterxml.jackson</wls:package-name>
         </wls:prefer-application-packages>
     </wls:container-descriptor>
</wls:weblogic-web-app> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM