spring 4.3.7和weblogic 12.2.1 Rest集成有错误

时间:2017-04-20 07:38:18

标签: java spring rest weblogic12c

我有一个宁静的春季网络应用程序,我想部署在weblogic.deploying这在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

我尝试过不同的jackson lib版本:2.6 2.7 2.7.6 2.8 2.8.7 2.8.8和2.9但它有相同的错误。 我的控制器类:     包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);
}

}

应用程序成功部署,但是当我发布http://localhost:7001/user/时,我在weblogic中收到了该错误。

3 个答案:

答案 0 :(得分:1)

NoSuchMethodError表示该类不再具有该方法的定义。这可能是由于Weblogic调用的库版本与应用程序的预期版本不同。

查找jar

  1. 暂时向服务器的JVM添加-verbose:class参数 参数。
  2. 在服务器的输出文件中,您将能够看到* .jar 正在加载。
  3. 如果jar版本与您预期的版本不同,您可以考虑将应用程序升级到较新版本的库。如果这不可行,请使用此parameter进行小规模的解决方法。

答案 1 :(得分:0)

它似乎是一个类加载器问题。 尝试通过配置来隔离webapp类加载器,以便首先使用webapp类加载器,然后再使用父类加载器

答案 2 :(得分:0)

尝试将weblogic.xml文件添加到您的应用程序中。在此文件中,添加标记prefer-application-packages whith package 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>