2014-09-25 20 views
1

我發現很多示例(實際上都是)圍繞像這個帖子那樣的@ResponseBody進行設計(click here)。然而,我似乎無法將這一切與許多看似矛盾的方式結合起來做一件簡單的事情。我承認,我最後一次使用Spring的次數限制爲2.5次,不幸的是,這使得我和前一版本一樣好。我必須爲這個項目編寫我自己的服務,而Spring顯然是要走的路。我使用了一組優秀的示例(click here)將最初的概念放在一起,並且我的Sencha Touch應用程序中的所有代碼都能正常工作。或者我想。直到我開始PUTing並向控制器發佈JSON有效負載。突然間沒有從HTTP請求映射的數據。未解析的JSON有效負載Spring 4.x MVC

這裏是我的控制器,請記住,這是春天4,並根據我讀@ReponseBody在春季3中使用,目前該功能委派,因爲用來代替@Controller了Spring 3 @RestController的所以對於像我這樣的人來說,配置細節是粗略的,對Java有很好的把握,但對於像Spring這樣的框架沒有深入的經驗。我向您詢問有關「bean」的配置位置的詳細信息,因爲使用2.5時,它是一個用於配置的XML動物園。似乎命名約定是現在的一個規則,對於以前留給XML文件的關聯。他們過去在某種程度上,但不像他們現在看起來那樣。

 @RequestMapping(value = "/create/{wellTestID}", method = RequestMethod.POST,headers="Accept=application/json") 
    public List<WellTest> getWellTestByWellTestID(@PathVariable Long wellTestID, 
      @RequestParam(defaultValue = "99999") Long wellFacilityID, 
      @RequestParam(defaultValue = "101") int operatorPersonID, 
      @RequestParam(defaultValue = "67845") int durationSeconds, 
      @RequestParam(defaultValue = "100") long grossRate, 
      @RequestParam(defaultValue = "400") long waterCut, 
      @RequestParam(defaultValue = "30")  long percentGas, 
      @RequestParam(defaultValue = "500") long temperature, 
      @RequestParam(defaultValue = "60")  long flowLinePressure, 
      @RequestParam(defaultValue = "50")  long casingPressure, 
      @RequestParam(defaultValue = "0")  int allocation, 
      @RequestParam(defaultValue = "3")  int wellTestTypeID, 
      @RequestParam(defaultValue = "you") String createUser, 
      @RequestParam(defaultValue = "me-me") String lastModifiedUser, 
      @RequestParam(defaultValue = "1-1-2014") String startDate, 
      @RequestParam(defaultValue = "1-1-2014") String createDate, 
      @RequestParam(defaultValue = "1-1-2014") String lastModifiedDate, 
      WellTest wellTest) throws ParseException { 
     wellTestService.createWellTest(wellTest); 
     List<WellTest> wt = wellTestService.getWellTestByWellTestID(wellTestID); 
     return wt; 
    } 

我能得到這個拿起PARAMS的唯一方法是通過連接的請求參數到URL,這是我們都知道是錯誤的。但花了幾天的時間來確定問題後,我會採取任何可行的方法。我使用的測試工具確實無法幫助我爲這個問題做好準備,因爲發佈JSON負載與表單變量不同。

這就是問題所在,如果有人遇到這種異常情況,我的希望是專家將在這裏回答一個完整的和演示的解決方案。

問題是:如何使用傑克遜來解析和綁定POST上的JSON負載(對於W3C建議,PUT的工作原理是相同的) - 如果這有所作爲,我將使用Glassfish 4.0。

我有一個讓人們在使用這些「免費」例子時需要考慮的問題,我們很多人都從這些例子開始。這個問題總是,我的意思是總是運行任何使用服務的演示軟件。在我無法完成這項工作之後,我做了一些工作,發現更新實際上是GET的變量模式,我發現它很奇怪,但在被請求之前被連接到URL。我花了太多的時間嘗試學習這個知識來實現​​它,但我無法及時做到這一點。

感謝所有關於這篇文章的答案。你有我的感激之情。

柯蒂斯費舍爾

我收到一個答案,使更多的問題,而不是導致我更接近答案。我很欣賞它,但是我無法用這種知識片段來實現任何東西。

下面是如何豆加載中...

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
<context:component-scan base-package="com.aspecteg.springservice.controller" /> 
<mvc:annotation-driven /> 
</beans> 

嗯,我做編輯戴維斯女士的職位的錯誤,我問的問題走進醚所謂同行評審。毫無疑問,我會因爲一些違規而受到質疑。

首先,我想感謝你和薩姆納先生的回答。我的下一個問題是因爲我注意到女士戴維斯的例子建立在薩姆納先生的建議,它似乎在這兩個JSON和java的(WellTest)模型對象是自動由

MyObject res = myObjectRepository.save(myObject);

行代碼填充。現在這看起來很酷。但是我有幾個問題...

什麼樣的對象是...

@Autowired 
private MyObjectRepository myObjectRepository; 

以及它是如何在「有線」?

@ResponseStatus(HttpStatus.CREATED) 

什麼是@ResponseStatus負責?

什麼是您正在使用的Logger,Log4J?

鑑於下面的代碼,你們中的任何一個有任何更正?

@RestController 
@RequestMapping("/service/welltest") 
public class WellTestController { 

private static Logger LOG = LoggerFactory.getLogger(MyObjectsController.class); 

@Autowired 
private MyObjectRepository myObjectRepository; //NEED MORE INFO ON THIS - Curtis 

@RequestMapping(value = "/update/{wellTestID}", method = RequestMethod.PUT,headers="Accept=application/json", 
        consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 
@ResponseStatus(HttpStatus.CREATED) 
public WellTest createUserPayment(@RequestBody WellTest wellTest) { 
    WellTest wt = myObjectRepository.save(wellTest); 
    LOG.info("the result of the save: "+wt.toString()); 
    return res; 
} 
} 

最後,因爲我已經通過一個XML文件中加載我的豆子,不與註解的類,有沒有傑克遜豆或需要在定義中加載的對象?或者其他配置細節?只要我從你們任何一方聽到我的問題,我會執行這個。如果我一段時間都沒有聽到您的消息,我會嘗試對Sumner先生的解決方案進行一些變更,因爲我不知道如何實施Davis女士的@autoWired部分。我準備好了!

謝謝兩位...

柯蒂斯費舍爾

+0

我不明白你的問題。如果你的類路徑上有Jackson庫,並且在你的servlet上下文@ @ Configuration'類中使用了默認的'@ EnabledWebMvc',你只需用'@ RequestBody'註釋你的'@ Controller'處理方法POJO類型參數。它會將請求的主體反序列化爲POJO。 – 2014-09-25 14:25:02

+0

您還沒有收到任何答案,只有評論。你沒有收到答案,因爲你的問題過於寬泛。你需要關注一個單一的問題,陳述它,並陳述你的嘗試。你不是在寫一封電子郵件,而是在問你一個問題。請參閱http://stackoverflow.com/help/how-to-ask – 2014-09-25 20:25:10

回答

2

我不完全是因爲你沒有提供什麼樣的問題,實際上是一個明確的指示理解您的問題。話雖這麼說,下面的應該只是罰款:

@RestController 
class FooController { 
    @RequestMapping(
    value = "/some/endpoint", 
    method = RequestMethod.POST, 
    consumes = MediaType.APPLICATION_JSON_VALUE, 
    produces = MediaType.APPLICATION_JSON_VALUE 
) 
    public MyResponseObject someEndpoint(@RequestBody MyRequestObject requestBody) 
    { 
    MyResponseObject responseObject = new MyResponseObject(); 
    responseObject.setSomeProperty("foo"); 
    responseObject.setAnotherProperty(responseObject.getSomeProperty("bar")); 

    return responseObject; 
    } 
} 
+0

謝謝薩默斯先生提供此信息,我認爲我通過編輯戴維斯女士的回答來答覆你們兩個人是錯誤的。它說,在任何人都可以閱讀之前,它已經開始進行同行評審了。其他人可能會告訴我我沒有做對 - 論壇。我保證會更好。這將需要一些練習!另外,我很高興我用「解析」這個工作來描述我的問題,即使它不那麼清楚,我也注意到了......再次感謝! – 2014-09-25 22:59:53

+0

我繼續前進,並使用您的示例,它完美的映射。我有我需要繼續Sencha應用這個概念。謝謝你們,這正是我所需要的。 – 2014-09-26 17:01:37

0

這是使用RestController來處理POST請求的例子。您的JSON只需包含MyObject類中所需的字段。讓我知道你是否需要額外的澄清。

@RestController 
@RequestMapping(value = "/myPath") 
public class MyObjectsController { 

    private static Logger LOG = LoggerFactory.getLogger(MyObjectsController.class); 

    @Autowired 
    private MyObjectRepository myObjectRepository; 

    @RequestMapping(value = "", method = POST) 
    @ResponseStatus(HttpStatus.CREATED) 
    public MyObject createUserPayment(@RequestBody MyObject myObject) { 
     MyObject res = myObjectRepository.save(myObject); 
     LOG.info("the result of the save: "+res); 
     return res; 
    } 
}