spring mvc Always JSON repsonse - spring-mvc

I'm trying to learn Spring MVC and i'm stuck at this program behaving weirdly can any one please help.
#Controller
public class GreetingController {
#RequestMapping(value="/greeting", consumes={"application/json", "application/xml"},produces = {"application/json","application/xml"}, headers = "Content-type=*/*")
public #ResponseBody Greeting greeting(
#RequestParam(value="name", required=false, defaultValue="I'm default") String name) {
Greeting obj = new Greeting("1",name);
Address address = new Address("CA, US");
obj.setAddress(address);
return obj;
}
}
Greeting and Address are just POJO's.
here is the output from 'curl' i.e irrespective of Content-type output is in JSON format.
curl --header "Content-type: application/xml" http://host.com:8080/javamvc/greeting
{"id":1,"content":"Hello, I'm default!","address":{"addr":"CA, US"}}
curl --header "Content-type: application/json" http://host.com:8080/javamvc/greeting
{"id":1,"content":"Hello, I'm default!","address":{"addr":"CA, US"}}
and then when i use 'RestClient' from mozilla i get output as xml always as irrespective of my Content-type=application/json or Content-type=application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><greeting><id>1</id><content>Hello, I'm default!</content><address><addr>CA, US</addr></address></greeting>
Can anyone please help?

In your curl requests, you are specifying the Content-Type of the request body, not the content type you are expecting in the response body.
For that, you need to specify the Accept header.
Spring is always producing application/json, because it's the first in the list of produces values.
You'll have to be more specific about what you are doing with Mozilla, but it seems like it's requesting with the Accept header being application/xml.

Related

Spring 5 - Read JSON or MultipartFile

I ran into what looks like a really simple problem. I have an http endpoint which should accept either JSON body or uploaded file.
Here is definition of controller method:
#PostMapping(value = "/api/endpoint")
public CompletableFuture<ResponseEntity<Void>> createResource(
#RequestParam(name = "file", required = false) MultipartFile file,
#RequestBody(required = false) Command command){
}
Command is a POJO class with Jackson annotations for deserialisation.
When I pass JSON body with Content-Type: application/json, it works fine. But when I pass file with Content-Type: multipart/form-data, I get 415 Unsupported Media Type.
Here is raw http request when passing just JSON body.
POST /api/devices?= HTTP/1.1
Host: localhost:8080
Content-Type: application/json
cache-control: no-cache
{"foo": "bar"}------WebKitFormBoundary7MA4YWxkTrZu0gW--
Any ideas what is causing this, thanks.
Try the below code.
#PostMapping(value = "/api/endpoint",consumes = {"multipart/form-data"})
public CompletableFuture<ResponseEntity<Void>> createResource(
#RequestPart("file") MultipartFile file,
#RequestPart Command command){
}
You might need two separate methods with different parameters and annotations. Which, of course, can each just call a shared internal method for their common behaviors.

Unexpected Content type in response on using Java RestAssured

I had set Content-Type in RequestSpecBuilder as "ContentType.JSON". But on making a GET request, I get Content-Type as "application/xml" in response. How do i get back a json response?
I have tried below approaches:
1. Set content type in RequestSpecBuilder object using setContentType method of RequestSpecBuilder class to "ContentType.JSON" and pass RequestSpecBuilder object in spec method of RequestSpecification --- got "application/xml" in response
Set content type in RequestSpecification object using contentType method of RequestSpecification and pass ContentType.JSON as parameter --- still got "application/xml" in response
Note: The webservice URL requires ".json" to be explicitly specified to get a json response else by default it returns a "xml" response. However, I wanted to set content type by using RequestSpecBuilder.
Eg:
for Json response: URL -- http://ergast.com/api/f1/2017/circuits.json
for Xml response: URL -- http://ergast.com/api/f1/2017/circuits
Code:
#Test
public void test_AddHeader() {
//Use of RequestSpecification
String pathUrl = "http://ergast.com/api/f1/2017/circuits";
RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
requestSpecBuilder = requestSpecBuilder.
setBaseUri(pathUrl).
setContentType(ContentType.JSON).
addQueryParam("limit", "10"); //added query param
RequestSpecification addRequestSpec = requestSpecBuilder.build();
RequestSpecification httpRequest = RestAssured.given().spec(addRequestSpec).contentType(ContentType.JSON);
Response httpResponse = httpRequest.get();
System.out.println(httpResponse.getContentType()); //returns application/xml
System.out.println(httpResponse.getStatusLine()); //returns HTTP/1.1 200 OK
System.out.println(httpResponse.getBody().asString());//returns XML response
}
You are expecting JSON from Response but you are passing setContentType to your RequestSpecBuilder. This will just create your POST payload in json format. It does not do anything to your response.
What you can do instead is Create a ResponseBuilder and do a setContentType to JSON there. Hope this will help you.

Exception on JSON-PATCH post to #RepositoryRestResource in Spring Data REST

I can't get Spring Data REST to accept a JSON-PATCH content body, what am I doing wrong?
Simple domain:
#Document
public class DomainA {
#Id private String id;
private String atext;
public String getAtext(){ return atext;}
public void setAtext(String str){ atext = str;}
}
Backed by a MondoDB repo:
#RepositoryRestResource
public interface DomainARepository extends MongoRepository<DomainA, String> {}
I want to JSON-PATCH this,like so:
curl -X PATCH -H "Authorization: Basic dXNlcjpQVw=="
-H "Content-Type: application/json-patch+json"
-H "Cache-Control: no-cache"
-d '[{"op":"replace", "path":"/atext", "value":"JSON-PATCHed text"}]'
http://localhost:8080/domainAs/550e169209a5cc0df82c95d4
But when I do I get an exception:
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read an object of type class patchex.DomainA from the request!;
nested exception is
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read payload!; nested exception is java.lang.ClassCastException:
com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to
com.fasterxml.jackson.databind.node.ObjectNode at
org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:183)
....
If I supply the patch body without the array brackets, I get back a 204; no exception but no change to the document, either. I can "patch" the document using PUT or PATCH of {"atext":"PUTed value"}; POSTs/GETs work fine; and, the tests in Spring Data REST seem to verify that JSON-PATCH should work if I can get the content body accepted. So, what's the right way to using PATCH here?
(Using Spring Boot 1.2.2.RELEASE starters for -data-mongodb, -data-rest, java 1.8)
EDIT: Same exception if I back with a JPA (H2) CRUD #RepositoryRestResource repo.
EDIT: Opened JIRA at https://jira.spring.io/browse/DATAREST-498

spring-mvc The request sent by the client was syntactically incorrect. for json post

I am trying to build a springmvc app. On one of the action I want it to take a POST of JSON content. I am sending the following values:
POST /TestResults/create.json HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
[ { "test_name": "value1", "test_time": "111"} ]
However, when I send it I am getting the following error:
The request sent by the client was syntactically incorrect.
My action looks like this:
#RequestMapping(value="/create", method=RequestMethod.POST, consumes = "application/json")
#ResponseBody
public ModelAndView createTest(#RequestBody TestResult test_result) {
When I change the action to bepublic ModelAndView createTest(#RequestBody String test_result) { the action does succeed but I am using "String" at that time.
Is it possible to know what I might be doing incorrectly?
The JSON you are receiving is a JSON array. A JSON array unless you have a custom deserializer cannot be mapped to a type like TestResult, unless it's some subtype of Collection. You'll want to use something like
public ModelAndView createTest(#RequestBody List<TestResult> testResults) {
For #RequestBody String it works because all Spring has to do is read from the request body and convert the bytes into a String.

How can I send a REST XML POST request via curl?

Basically, I have a project set up in Restlet which uses JAXRS for mapping resources to paths and uses JAXB for serializing and deserializing XML to/from Java types. I'm currently trying to send a POST request in order to test whether it works, and I'm running into a bit of trouble. Here's my resource:
#Path("stream")
public class StreamResource {
#POST
#Consumes("text/xml")
#Produces("text/xml")
public Stream save(Stream value) {
logger.debug("saving new stream...");
return (Stream)this.streamPersistence.save(value);
}
}
Here's my Stream class:
#XmlRootElement(name="stream")
#XmlType(propOrder={"id", "streamName", "title", "description", fileSystemPath"})
public class Stream {
private Long id;
private String streamName;
private String fileSystemPath;
private String title;
private String description;
// getters/setters omitted for brevity
}
And here's how I'm invoking curl:
curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type:"text/xml"' http://localhost:8888/stream
Here's the error I'm getting from curl:
The given resource variant is not supported.
...and here's the error in Restlet:
15:02:25.809 [Restlet-961410881] WARN org.restlet.Component.Server - Error while parsing entity headers java.lang.IllegalArgumentException: Illegal token: "text
at org.restlet.data.MediaType.normalizeToken(MediaType.java:647)
at org.restlet.data.MediaType.normalizeType(MediaType.java:686)
at org.restlet.data.MediaType.<init>(MediaType.java:795)
at org.restlet.data.MediaType.<init>(MediaType.java:767)
at org.restlet.engine.http.header.ContentTypeReader.createContentType(ContentTypeReader.java:84)
at org.restlet.engine.http.header.ContentTypeReader.readValue(ContentTypeReader.java:112)
at org.restlet.engine.http.header.ContentType.<init>(ContentType.java:99)
at org.restlet.engine.http.header.HeaderUtils.extractEntityHeaders(HeaderUtils.java:664)
at org.restlet.engine.http.connector.Connection.createInboundEntity(Connection.java:313)
at org.restlet.engine.http.connector.ServerConnection.createRequest(ServerConnection.java:136)
at org.restlet.engine.http.connector.ServerConnection.readMessage(ServerConnection.java:229)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:673)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
What am I doing wrong here? This seems pretty straightforward, right?
Remove the quotes around text/xml.
In other words you want
curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type: text/xml' http://localhost:8888/stream
curl -v -H "Content-Type: application/xml" -X POST --data-binary "#token.xml" URL

Resources