Я использую Spring 5 MVC, чтобы ответить JSON на запрос don с JQuery.
Код клиента такой:
$.ajax({
url: 'http://127.0.0.1:8080/spring.MVC.REST.simple-0.0.1-SNAPSHOT/Products.htm',
dataType: 'json',
type: 'GET',
success: function(result){
$('#resultados').html(result);
}
})
Ожидаемый заголовок запроса, какэто:
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9,en;q=0.8
Connection: keep-alive
Host: 127.0.0.1:8080
Referer: http://127.0.0.1:8080/spring.MVC.REST.simple-0.0.1-SNAPSHOT/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
X-Requested-With: XMLHttpRequest
Так что я думаю, что проблема на стороне сервера.Я использую WildFly 14, а заголовок ответа выглядит следующим образом:
Connection: keep-alive
Content-Length: 73
Content-Type: text/html;charset=UTF-8
Date: Fri, 19 Oct 2018 14:09:40 GMT
Я пытался отправить с сервера Content-Type: appliation / json, но клиент получил Content-Type: text / html (которыйне принимается).
На сервере у меня есть контроллер.Здесь я поместил фрагмент кода:
@Controller
@RequestMapping("/Products.htm")
public class ProductsController {
@Autowired
private ProductManager productManager;
@RequestMapping(method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE})
@ResponseBody
public List<Product> getProducts() {
return productManager.getProducts();
}
....
Также у меня в классе WebConfig есть бин для определения содержания между взаимодействиями http.Здесь есть фрагмент кода:
@Configuration
@EnableWebMvc
@ComponentScan("es.foxinet.springapp.web")
public class SpringappWebConfig {
@Bean
public ContentNegotiationManagerFactoryBean
contentNegotiationManager() {
ContentNegotiationManagerFactoryBean negociatorBean = new
ContentNegotiationManagerFactoryBean();
negociatorBean.setFavorPathExtension(false);
negociatorBean.setParameterName("mediaType");
negociatorBean.setFavorParameter(true);
negociatorBean.setIgnoreAcceptHeader(true);
negociatorBean.setUseRegisteredExtensionsOnly(true);
negociatorBean.setDefaultContentType(MediaType.APPLICATION_JSON);
negociatorBean.addMediaType("json", MediaType.APPLICATION_JSON);
negociatorBean.addMediaType("xml", MediaType.APPLICATION_XML);
return negociatorBean;
}
.....
}
Также в моем файле Maven pon.xml есть следующие зависимости для работы с Джексоном (для JSON) и JAXB (для XML).
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
<scope>runtime</scope>
</dependency>
Что я делаю плохо?
Буду признателен вам за помощь.Заранее спасибо