2013-11-23 85 views
1

我從REST技術開始,選擇Spring 3.2和Jackson 2.2。我有一個小問題。我創建了REST API,它看起來像這樣:Spring 3.2 Jackson 2.2 REST API

@Controller 
public class WorkersController { 

@Autowired 
public DatabaseService dbService; 

@ResponseBody 
@RequestMapping(value="/workers", method = RequestMethod.GET, produces = "application/json") 
public ArrayList<Worker> getAllWorkersFromDatabase() { 
} 

@ResponseBody 
@RequestMapping(value="/workers/new", method = RequestMethod.POST, produces = "application/json", consumes="application/json") 
public String saveWorker(@RequestBody final WorkerDTO workerDto) { 
} 

@ResponseBody 
@RequestMapping(value="/workers/{workerid}", method = RequestMethod.GET, produces = "application/json") 
public Worker getWOrkerByDatabaseId(@PathVariable Integer workerid) { 
} 

@ResponseBody 
@RequestMapping(value="/workers/{workerid}/edit", method = RequestMethod.PUT, produces = "application/json") 
public String editWorker(@PathVariable Integer workerid, @RequestBody Worker worker) { 
} 
} 

當我使HTTP GET一切正常但我有POST的問題。當我打電話saveWorker()方法獲得:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method 

我進口需要的庫:

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>${jackson.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>${jackson.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-annotations</artifactId> 
     <version>${jackson.version}</version> 
    </dependency> 

我認爲主要的問題是在配置文件和@RequestBody着地圖JSON到DTO。這是我的配置:

@Configuration 
@ComponentScan(basePackages = "org.schedule.service") 
@EnableWebMvc 
@Import(DatabaseSpringConfig.class) 
public class ServiceSpringConfig extends WebMvcConfigurationSupport{ 

@Override 
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 
    List<MediaType> jsonTypes = new ArrayList<>(jsonConverter.getSupportedMediaTypes()); 
    jsonTypes.add(MediaType.TEXT_PLAIN); 
    jsonTypes.add(MediaType.APPLICATION_JSON); 
    jsonConverter.setSupportedMediaTypes(jsonTypes); 
    converters.add(jsonConverter); 
} 

}

我的DTO:

public class WorkerDTO implements Serializable { 

private static final long serialVersionUID = 1L; 

public String name; 
public String surname; 

public WorkerDTO() { 
} 
} 

JSON:

{ 
"name": "asdssss", 
"surname": "asdssssss" 
} 

和HTTP調用:

localhost:8080/Schedule-service/workers/new?Content-type=application/json 

感謝您的回覆。

回答

3

請求

localhost:8080/Schedule-service/workers/new?Content-type=application/json 

具有名稱Content-Type和值application/json一個請求參數。

HttpMessageConverter類,特別是MappingJackson2HttpMessageConverter不尋找請求參數,它們查找標題。

您需要爲您的請求指定一個Content-Type標題。