2015-10-06 91 views
1
@RequestMapping(value = "/createItem", method = RequestMethod.POST) 
    @Override 
    public void createItem(@RequestParam(value="userId") String userId, @RequestParam(value="title") String title, @RequestParam(value="subtitle") String subtitle, @RequestParam(value="description") String description, @RequestParam(value="category") String category, @RequestParam(value="datapack") String datapack) { 
     this.itemDAO.createItem(userId, title, subtitle, description, category, datapack); 
} 

我正在用Spring創建RESTful應用程序。上面的方法工作得很好,但是當數據包長度超過一定長度時會導致錯誤。錯誤說...彈簧參數太長

解析HTTP請求標頭時出錯注意:在DEBUG級別將記錄進一步發生的HTTP 標頭解析錯誤。

我需要傳遞datapack作爲參數,datapack本身將是一個json文件,我將它轉換爲字符串。

datapack文件可能非常複雜而且很大。我該如何解決這個問題?

這裏的請求的例子:上述工程

http://localhost:8090/createItem?userId=test&title=test&subtitle=test&description=test&category=test&datapack= 
    { 
    "CLASS": "com.mincom.ellipse.edoi.ejb.menu_item.MENU_ITEMRec", 
    "INSTANCE": { 
     "m_creationDate": "20150824", 
     "m_creationTime": "001616", 
     "m_creationUser": "SR4187", 
     "m_lastModDate": "20150824", 
     "m_lastModTime": "001616", 
     "m_lastModUser": "SR4187", 
     "m_menuType": "", 
     "m_invokationString": "", 
     "primaryKey": { 
     "m_uuid": "3b4d95fe3dd3432fb00cde0cc25f903f" 
     } 
    } 
    }, 
    { 
    "CLASS": "com.mincom.ellipse.edoi.ejb.i18n_descriptions.I18N_DESCRIPTIONSRec", 
    "INSTANCE": { 
     "m_creationDate": "20150824", 
     "m_creationTime": "001616", 
     "m_creationUser": "SR4187", 
     "m_lastModDate": "20150824", 
     "m_lastModTime": "001616", 
     "m_lastModUser": "SR4187", 
     "m_description": "CUSTOM_MENU", 
     "primaryKey": { 
     "m_locale": "en", 
     "m_uuid": "3b4d95fe3dd3432fb00cde0cc25f903f" 
     } 
    } 
    }, 
    { 
    "CLASS": "com.mincom.ellipse.edoi.ejb.top_level_menus.TOP_LEVEL_MENUSRec", 
    "INSTANCE": { 
     "m_creationDate": "20150824", 
     "m_creationUser": "SR4187", 
     "m_lastModDate": "20150824", 
     "m_lastModTime": "001620", 
     "m_creationTime": "001620", 
     "m_lastModUser": "SR4187", 
     "m_uuid": "3b4d95fe3dd3432fb00cde0cc25f903f", 
     "primaryKey": { 
     "m_name": "SX" 
     } 
    } 
    } 

例子,但如果我更長的JSON文件來測試,它不會工作。

+0

您使用的是什麼應用程序服務器?這是春季開機? –

+0

你如何形成你的http請求?你只是提交一個表單或由javaScript生成的莫名其妙? –

+0

無論如何,這些數據有多大?它是如此之大,它必須分成多個HTTP包? –

回答

0

創建應接收刊登的JSON數據的控制器方法$ HTTP使用XHR(AJAX)

@RequestMapping(value = "/savecompany_json", method = RequestMethod.POST) 
public @ResponseBody String saveCompany_JSON(@RequestBody Company company) {  
    // 
    // Code processing the input parameters 
    // 
    return "JSON: The company name: " + company.getName() + ", Employees count: " + company.getEmployees() + ", Headoffice: " + company.getHeadoffice(); 
} 

創建映射到JSON對象POJO服務

public class Company { 

    private String name; 
    private long employees; 
    private String headoffice; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public Long getEmployees() { 
     return employees; 
    } 
    public void setEmployees(Long employees) { 
     this.employees = employees; 
    } 
    public String getHeadoffice() { 
     return headoffice; 
    } 
    public void setHeadoffice(String headoffice) { 
     this.headoffice = headoffice; 
    } 
} 

Source

0

如果請求確實很大(例如發送文件時),您可能需要更改您的http頭中的內容類型。舉例來說(雖然看起來可能不同,這取決於你實際上發送):

$http({ 
    method: 'POST', 
    url: url, 
    headers: { 
     'Content-Type': 'multipart/form-data' 
    }, 
    data: { 
     data: model, 
     file: file 
    } 
}); 

然後,如果該請求是太大,服務器接受它,你可能需要延長請求的大小限制。如果您使用的是spring引導,則可以轉至application.properties文件(應該位於src/main/resources下,但如果它尚不存在,請手動創建它)並添加以下屬性:

multipart.maxFileSize=3MB 
multipart.maxRequestSize=3180KB 
+0

這是從角度的要求? – AndHer

+0

是的,這是你如何做角度的請求 –

0

無論如何,您正在使用POST請求,因此只需將正文中的參數與正常表單提交內容類型application/x-www-form-urlencoded一起發送,而不是在URL中。