ContentNegotiation: how to serve, except for the highest ranking, from the accept header

I have this Java Spring configuration with several custom HttpMessageConverters:

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorParameter(true).
            ignoreAcceptHeader(false).
            useJaf(true).
            defaultContentType(MediaType.TEXT_HTML).
            mediaType("html", MediaType.TEXT__HTML).
            mediaType("rdf", MediaTypes.RDFXML);
}

If I request this setup with Jena, I get an error message:

The resource specified in this request is capable of generating responses with characteristics that are not acceptable in accordance with the request to “accept” headers.

Jena sends a request with this Accept header:

Accept: text / turtle, application / n-threes; d = 0.9, application / TTO + XML; d = 0.8, application / XML; d = 0.7, /; d = 0.5

, application/rdf+xml . , . Spring 0,8- application/rdf+xml, text/turtle application/n-triples ?

?

+4
2

MVC, , , .

@RequestMapping, produces - , Content-Type, , . "" , , . , , , , .

@RequestMapping(value="/sparql/service", produces={"application/rdf+xml;charset=utf-8", MediaType.ALL_VALUE})
public @ResponseBody String serviceDescriptionAsRdfXml()
{
    return null; // something here
}

@RequestMapping( value="/sparql/service", produces={"text/turtle;charset=utf-8"} )
public @ResponseBody String serviceDescriptionAsTurtle( final HttpServletRequest request )
{
    return null; // something here
}

Content-Type

, MediaType , ResponseEntity, , Content-Type . .

@RequestMapping(value="/sparql/query", method=RequestMethod.GET)
public ResponseEntity<String> queryViaGet(@RequestHeader(value="Accept") final List<MediaType> contentTypes)
{
    MediaType.sortBySpecificityAndQuality(contentTypes);

    // Do some stuff to select your content type and generate your response
    final String results = null;
    final MediaType desiredType = null;

    // Create your REST response
    final HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(desiredType);
    return new ResponseEntity<String>(results, responseHeaders, HttpStatus.OK);
}
+1

ContentNegotiationConfigurer.mediaType(String, MediaType) - Accept, , .

, ContentNegotiationConfigurer ( ) , .

, , , ( ), , application/rdf+xml . Spring , application/rdf+xml ( ) Accept , .

+1

Source: https://habr.com/ru/post/1537675/


All Articles